perf_event_p4.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 = get_cpu();
  401. int rc = 0;
  402. u32 escr, cccr;
  403. /*
  404. * the reason we use cpu that early is that: if we get scheduled
  405. * first time on the same cpu -- we will not need swap thread
  406. * specific flags in config (and will save some cpu cycles)
  407. */
  408. cccr = p4_default_cccr_conf(cpu);
  409. escr = p4_default_escr_conf(cpu, event->attr.exclude_kernel,
  410. event->attr.exclude_user);
  411. event->hw.config = p4_config_pack_escr(escr) |
  412. p4_config_pack_cccr(cccr);
  413. if (p4_ht_active() && p4_ht_thread(cpu))
  414. event->hw.config = p4_set_ht_bit(event->hw.config);
  415. if (event->attr.type == PERF_TYPE_RAW) {
  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. }
  429. rc = x86_setup_perfctr(event);
  430. put_cpu();
  431. return rc;
  432. }
  433. static inline void p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc)
  434. {
  435. unsigned long dummy;
  436. rdmsrl(hwc->config_base + hwc->idx, dummy);
  437. if (dummy & P4_CCCR_OVF) {
  438. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  439. ((u64)dummy) & ~P4_CCCR_OVF);
  440. }
  441. }
  442. static inline void p4_pmu_disable_event(struct perf_event *event)
  443. {
  444. struct hw_perf_event *hwc = &event->hw;
  445. /*
  446. * If event gets disabled while counter is in overflowed
  447. * state we need to clear P4_CCCR_OVF, otherwise interrupt get
  448. * asserted again and again
  449. */
  450. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  451. (u64)(p4_config_unpack_cccr(hwc->config)) &
  452. ~P4_CCCR_ENABLE & ~P4_CCCR_OVF & ~P4_CCCR_RESERVED);
  453. }
  454. static void p4_pmu_disable_all(void)
  455. {
  456. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  457. int idx;
  458. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  459. struct perf_event *event = cpuc->events[idx];
  460. if (!test_bit(idx, cpuc->active_mask))
  461. continue;
  462. p4_pmu_disable_event(event);
  463. }
  464. }
  465. static void p4_pmu_enable_event(struct perf_event *event)
  466. {
  467. struct hw_perf_event *hwc = &event->hw;
  468. int thread = p4_ht_config_thread(hwc->config);
  469. u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config));
  470. unsigned int idx = p4_config_unpack_event(hwc->config);
  471. unsigned int idx_cache = p4_config_unpack_cache_event(hwc->config);
  472. struct p4_event_bind *bind;
  473. struct p4_cache_event_bind *bind_cache;
  474. u64 escr_addr, cccr;
  475. bind = &p4_event_bind_map[idx];
  476. escr_addr = (u64)bind->escr_msr[thread];
  477. /*
  478. * - we dont support cascaded counters yet
  479. * - and counter 1 is broken (erratum)
  480. */
  481. WARN_ON_ONCE(p4_is_event_cascaded(hwc->config));
  482. WARN_ON_ONCE(hwc->idx == 1);
  483. /* we need a real Event value */
  484. escr_conf &= ~P4_ESCR_EVENT_MASK;
  485. escr_conf |= P4_ESCR_EVENT(P4_OPCODE_EVNT(bind->opcode));
  486. cccr = p4_config_unpack_cccr(hwc->config);
  487. /*
  488. * it could be Cache event so that we need to
  489. * set metrics into additional MSRs
  490. */
  491. BUILD_BUG_ON(P4_CACHE__MAX > P4_CCCR_CACHE_OPS_MASK);
  492. if (idx_cache > P4_CACHE__NONE &&
  493. idx_cache < ARRAY_SIZE(p4_cache_event_bind_map)) {
  494. bind_cache = &p4_cache_event_bind_map[idx_cache];
  495. (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)bind_cache->metric_pebs);
  496. (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)bind_cache->metric_vert);
  497. }
  498. (void)checking_wrmsrl(escr_addr, escr_conf);
  499. (void)checking_wrmsrl(hwc->config_base + hwc->idx,
  500. (cccr & ~P4_CCCR_RESERVED) | P4_CCCR_ENABLE);
  501. }
  502. static void p4_pmu_enable_all(int added)
  503. {
  504. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  505. int idx;
  506. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  507. struct perf_event *event = cpuc->events[idx];
  508. if (!test_bit(idx, cpuc->active_mask))
  509. continue;
  510. p4_pmu_enable_event(event);
  511. }
  512. }
  513. static int p4_pmu_handle_irq(struct pt_regs *regs)
  514. {
  515. struct perf_sample_data data;
  516. struct cpu_hw_events *cpuc;
  517. struct perf_event *event;
  518. struct hw_perf_event *hwc;
  519. int idx, handled = 0;
  520. u64 val;
  521. data.addr = 0;
  522. data.raw = NULL;
  523. cpuc = &__get_cpu_var(cpu_hw_events);
  524. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  525. if (!test_bit(idx, cpuc->active_mask))
  526. continue;
  527. event = cpuc->events[idx];
  528. hwc = &event->hw;
  529. WARN_ON_ONCE(hwc->idx != idx);
  530. /*
  531. * FIXME: Redundant call, actually not needed
  532. * but just to check if we're screwed
  533. */
  534. p4_pmu_clear_cccr_ovf(hwc);
  535. val = x86_perf_event_update(event);
  536. if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
  537. continue;
  538. /*
  539. * event overflow
  540. */
  541. handled = 1;
  542. data.period = event->hw.last_period;
  543. if (!x86_perf_event_set_period(event))
  544. continue;
  545. if (perf_event_overflow(event, 1, &data, regs))
  546. p4_pmu_disable_event(event);
  547. }
  548. if (handled) {
  549. /* p4 quirk: unmask it again */
  550. apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
  551. inc_irq_stat(apic_perf_irqs);
  552. }
  553. return handled;
  554. }
  555. /*
  556. * swap thread specific fields according to a thread
  557. * we are going to run on
  558. */
  559. static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu)
  560. {
  561. u32 escr, cccr;
  562. /*
  563. * we either lucky and continue on same cpu or no HT support
  564. */
  565. if (!p4_should_swap_ts(hwc->config, cpu))
  566. return;
  567. /*
  568. * the event is migrated from an another logical
  569. * cpu, so we need to swap thread specific flags
  570. */
  571. escr = p4_config_unpack_escr(hwc->config);
  572. cccr = p4_config_unpack_cccr(hwc->config);
  573. if (p4_ht_thread(cpu)) {
  574. cccr &= ~P4_CCCR_OVF_PMI_T0;
  575. cccr |= P4_CCCR_OVF_PMI_T1;
  576. if (escr & P4_ESCR_T0_OS) {
  577. escr &= ~P4_ESCR_T0_OS;
  578. escr |= P4_ESCR_T1_OS;
  579. }
  580. if (escr & P4_ESCR_T0_USR) {
  581. escr &= ~P4_ESCR_T0_USR;
  582. escr |= P4_ESCR_T1_USR;
  583. }
  584. hwc->config = p4_config_pack_escr(escr);
  585. hwc->config |= p4_config_pack_cccr(cccr);
  586. hwc->config |= P4_CONFIG_HT;
  587. } else {
  588. cccr &= ~P4_CCCR_OVF_PMI_T1;
  589. cccr |= P4_CCCR_OVF_PMI_T0;
  590. if (escr & P4_ESCR_T1_OS) {
  591. escr &= ~P4_ESCR_T1_OS;
  592. escr |= P4_ESCR_T0_OS;
  593. }
  594. if (escr & P4_ESCR_T1_USR) {
  595. escr &= ~P4_ESCR_T1_USR;
  596. escr |= P4_ESCR_T0_USR;
  597. }
  598. hwc->config = p4_config_pack_escr(escr);
  599. hwc->config |= p4_config_pack_cccr(cccr);
  600. hwc->config &= ~P4_CONFIG_HT;
  601. }
  602. }
  603. /* ESCRs are not sequential in memory so we need a map */
  604. static const unsigned int p4_escr_map[ARCH_P4_TOTAL_ESCR] = {
  605. MSR_P4_ALF_ESCR0, /* 0 */
  606. MSR_P4_ALF_ESCR1, /* 1 */
  607. MSR_P4_BPU_ESCR0, /* 2 */
  608. MSR_P4_BPU_ESCR1, /* 3 */
  609. MSR_P4_BSU_ESCR0, /* 4 */
  610. MSR_P4_BSU_ESCR1, /* 5 */
  611. MSR_P4_CRU_ESCR0, /* 6 */
  612. MSR_P4_CRU_ESCR1, /* 7 */
  613. MSR_P4_CRU_ESCR2, /* 8 */
  614. MSR_P4_CRU_ESCR3, /* 9 */
  615. MSR_P4_CRU_ESCR4, /* 10 */
  616. MSR_P4_CRU_ESCR5, /* 11 */
  617. MSR_P4_DAC_ESCR0, /* 12 */
  618. MSR_P4_DAC_ESCR1, /* 13 */
  619. MSR_P4_FIRM_ESCR0, /* 14 */
  620. MSR_P4_FIRM_ESCR1, /* 15 */
  621. MSR_P4_FLAME_ESCR0, /* 16 */
  622. MSR_P4_FLAME_ESCR1, /* 17 */
  623. MSR_P4_FSB_ESCR0, /* 18 */
  624. MSR_P4_FSB_ESCR1, /* 19 */
  625. MSR_P4_IQ_ESCR0, /* 20 */
  626. MSR_P4_IQ_ESCR1, /* 21 */
  627. MSR_P4_IS_ESCR0, /* 22 */
  628. MSR_P4_IS_ESCR1, /* 23 */
  629. MSR_P4_ITLB_ESCR0, /* 24 */
  630. MSR_P4_ITLB_ESCR1, /* 25 */
  631. MSR_P4_IX_ESCR0, /* 26 */
  632. MSR_P4_IX_ESCR1, /* 27 */
  633. MSR_P4_MOB_ESCR0, /* 28 */
  634. MSR_P4_MOB_ESCR1, /* 29 */
  635. MSR_P4_MS_ESCR0, /* 30 */
  636. MSR_P4_MS_ESCR1, /* 31 */
  637. MSR_P4_PMH_ESCR0, /* 32 */
  638. MSR_P4_PMH_ESCR1, /* 33 */
  639. MSR_P4_RAT_ESCR0, /* 34 */
  640. MSR_P4_RAT_ESCR1, /* 35 */
  641. MSR_P4_SAAT_ESCR0, /* 36 */
  642. MSR_P4_SAAT_ESCR1, /* 37 */
  643. MSR_P4_SSU_ESCR0, /* 38 */
  644. MSR_P4_SSU_ESCR1, /* 39 */
  645. MSR_P4_TBPU_ESCR0, /* 40 */
  646. MSR_P4_TBPU_ESCR1, /* 41 */
  647. MSR_P4_TC_ESCR0, /* 42 */
  648. MSR_P4_TC_ESCR1, /* 43 */
  649. MSR_P4_U2L_ESCR0, /* 44 */
  650. MSR_P4_U2L_ESCR1, /* 45 */
  651. };
  652. static int p4_get_escr_idx(unsigned int addr)
  653. {
  654. unsigned int i;
  655. for (i = 0; i < ARRAY_SIZE(p4_escr_map); i++) {
  656. if (addr == p4_escr_map[i])
  657. return i;
  658. }
  659. return -1;
  660. }
  661. static int p4_next_cntr(int thread, unsigned long *used_mask,
  662. struct p4_event_bind *bind)
  663. {
  664. int i = 0, j;
  665. for (i = 0; i < P4_CNTR_LIMIT; i++) {
  666. j = bind->cntr[thread][i++];
  667. if (j == -1 || !test_bit(j, used_mask))
  668. return j;
  669. }
  670. return -1;
  671. }
  672. static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
  673. {
  674. unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
  675. unsigned long escr_mask[BITS_TO_LONGS(ARCH_P4_TOTAL_ESCR)];
  676. int cpu = raw_smp_processor_id();
  677. struct hw_perf_event *hwc;
  678. struct p4_event_bind *bind;
  679. unsigned int i, thread, num;
  680. int cntr_idx, escr_idx;
  681. bitmap_zero(used_mask, X86_PMC_IDX_MAX);
  682. bitmap_zero(escr_mask, ARCH_P4_TOTAL_ESCR);
  683. for (i = 0, num = n; i < n; i++, num--) {
  684. hwc = &cpuc->event_list[i]->hw;
  685. thread = p4_ht_thread(cpu);
  686. bind = p4_config_get_bind(hwc->config);
  687. escr_idx = p4_get_escr_idx(bind->escr_msr[thread]);
  688. if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) {
  689. cntr_idx = hwc->idx;
  690. if (assign)
  691. assign[i] = hwc->idx;
  692. goto reserve;
  693. }
  694. cntr_idx = p4_next_cntr(thread, used_mask, bind);
  695. if (cntr_idx == -1 || test_bit(escr_idx, escr_mask))
  696. goto done;
  697. p4_pmu_swap_config_ts(hwc, cpu);
  698. if (assign)
  699. assign[i] = cntr_idx;
  700. reserve:
  701. set_bit(cntr_idx, used_mask);
  702. set_bit(escr_idx, escr_mask);
  703. }
  704. done:
  705. return num ? -ENOSPC : 0;
  706. }
  707. static __initconst const struct x86_pmu p4_pmu = {
  708. .name = "Netburst P4/Xeon",
  709. .handle_irq = p4_pmu_handle_irq,
  710. .disable_all = p4_pmu_disable_all,
  711. .enable_all = p4_pmu_enable_all,
  712. .enable = p4_pmu_enable_event,
  713. .disable = p4_pmu_disable_event,
  714. .eventsel = MSR_P4_BPU_CCCR0,
  715. .perfctr = MSR_P4_BPU_PERFCTR0,
  716. .event_map = p4_pmu_event_map,
  717. .max_events = ARRAY_SIZE(p4_general_events),
  718. .get_event_constraints = x86_get_event_constraints,
  719. /*
  720. * IF HT disabled we may need to use all
  721. * ARCH_P4_MAX_CCCR counters simulaneously
  722. * though leave it restricted at moment assuming
  723. * HT is on
  724. */
  725. .num_counters = ARCH_P4_MAX_CCCR,
  726. .apic = 1,
  727. .cntval_bits = 40,
  728. .cntval_mask = (1ULL << 40) - 1,
  729. .max_period = (1ULL << 39) - 1,
  730. .hw_config = p4_hw_config,
  731. .schedule_events = p4_pmu_schedule_events,
  732. };
  733. static __init int p4_pmu_init(void)
  734. {
  735. unsigned int low, high;
  736. /* If we get stripped -- indexig fails */
  737. BUILD_BUG_ON(ARCH_P4_MAX_CCCR > X86_PMC_MAX_GENERIC);
  738. rdmsr(MSR_IA32_MISC_ENABLE, low, high);
  739. if (!(low & (1 << 7))) {
  740. pr_cont("unsupported Netburst CPU model %d ",
  741. boot_cpu_data.x86_model);
  742. return -ENODEV;
  743. }
  744. memcpy(hw_cache_event_ids, p4_hw_cache_event_ids,
  745. sizeof(hw_cache_event_ids));
  746. pr_cont("Netburst events, ");
  747. x86_pmu = p4_pmu;
  748. return 0;
  749. }
  750. #endif /* CONFIG_CPU_SUP_INTEL */