perf_event_p4.c 28 KB

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