op_model_p4.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /**
  2. * @file op_model_p4.c
  3. * P4 model-specific MSR operations
  4. *
  5. * @remark Copyright 2002 OProfile authors
  6. * @remark Read the file COPYING
  7. *
  8. * @author Graydon Hoare
  9. */
  10. #include <linux/oprofile.h>
  11. #include <linux/smp.h>
  12. #include <linux/ptrace.h>
  13. #include <asm/nmi.h>
  14. #include <asm/msr.h>
  15. #include <asm/fixmap.h>
  16. #include <asm/apic.h>
  17. #include "op_x86_model.h"
  18. #include "op_counter.h"
  19. #define NUM_EVENTS 39
  20. #define NUM_COUNTERS_NON_HT 8
  21. #define NUM_ESCRS_NON_HT 45
  22. #define NUM_CCCRS_NON_HT 18
  23. #define NUM_CONTROLS_NON_HT (NUM_ESCRS_NON_HT + NUM_CCCRS_NON_HT)
  24. #define NUM_COUNTERS_HT2 4
  25. #define NUM_ESCRS_HT2 23
  26. #define NUM_CCCRS_HT2 9
  27. #define NUM_CONTROLS_HT2 (NUM_ESCRS_HT2 + NUM_CCCRS_HT2)
  28. #define OP_CTR_OVERFLOW (1ULL<<31)
  29. static unsigned int num_counters = NUM_COUNTERS_NON_HT;
  30. static unsigned int num_controls = NUM_CONTROLS_NON_HT;
  31. /* this has to be checked dynamically since the
  32. hyper-threadedness of a chip is discovered at
  33. kernel boot-time. */
  34. static inline void setup_num_counters(void)
  35. {
  36. #ifdef CONFIG_SMP
  37. if (smp_num_siblings == 2) {
  38. num_counters = NUM_COUNTERS_HT2;
  39. num_controls = NUM_CONTROLS_HT2;
  40. }
  41. #endif
  42. }
  43. static inline int addr_increment(void)
  44. {
  45. #ifdef CONFIG_SMP
  46. return smp_num_siblings == 2 ? 2 : 1;
  47. #else
  48. return 1;
  49. #endif
  50. }
  51. /* tables to simulate simplified hardware view of p4 registers */
  52. struct p4_counter_binding {
  53. int virt_counter;
  54. int counter_address;
  55. int cccr_address;
  56. };
  57. struct p4_event_binding {
  58. int escr_select; /* value to put in CCCR */
  59. int event_select; /* value to put in ESCR */
  60. struct {
  61. int virt_counter; /* for this counter... */
  62. int escr_address; /* use this ESCR */
  63. } bindings[2];
  64. };
  65. /* nb: these CTR_* defines are a duplicate of defines in
  66. event/i386.p4*events. */
  67. #define CTR_BPU_0 (1 << 0)
  68. #define CTR_MS_0 (1 << 1)
  69. #define CTR_FLAME_0 (1 << 2)
  70. #define CTR_IQ_4 (1 << 3)
  71. #define CTR_BPU_2 (1 << 4)
  72. #define CTR_MS_2 (1 << 5)
  73. #define CTR_FLAME_2 (1 << 6)
  74. #define CTR_IQ_5 (1 << 7)
  75. static struct p4_counter_binding p4_counters[NUM_COUNTERS_NON_HT] = {
  76. { CTR_BPU_0, MSR_P4_BPU_PERFCTR0, MSR_P4_BPU_CCCR0 },
  77. { CTR_MS_0, MSR_P4_MS_PERFCTR0, MSR_P4_MS_CCCR0 },
  78. { CTR_FLAME_0, MSR_P4_FLAME_PERFCTR0, MSR_P4_FLAME_CCCR0 },
  79. { CTR_IQ_4, MSR_P4_IQ_PERFCTR4, MSR_P4_IQ_CCCR4 },
  80. { CTR_BPU_2, MSR_P4_BPU_PERFCTR2, MSR_P4_BPU_CCCR2 },
  81. { CTR_MS_2, MSR_P4_MS_PERFCTR2, MSR_P4_MS_CCCR2 },
  82. { CTR_FLAME_2, MSR_P4_FLAME_PERFCTR2, MSR_P4_FLAME_CCCR2 },
  83. { CTR_IQ_5, MSR_P4_IQ_PERFCTR5, MSR_P4_IQ_CCCR5 }
  84. };
  85. #define NUM_UNUSED_CCCRS (NUM_CCCRS_NON_HT - NUM_COUNTERS_NON_HT)
  86. /* p4 event codes in libop/op_event.h are indices into this table. */
  87. static struct p4_event_binding p4_events[NUM_EVENTS] = {
  88. { /* BRANCH_RETIRED */
  89. 0x05, 0x06,
  90. { {CTR_IQ_4, MSR_P4_CRU_ESCR2},
  91. {CTR_IQ_5, MSR_P4_CRU_ESCR3} }
  92. },
  93. { /* MISPRED_BRANCH_RETIRED */
  94. 0x04, 0x03,
  95. { { CTR_IQ_4, MSR_P4_CRU_ESCR0},
  96. { CTR_IQ_5, MSR_P4_CRU_ESCR1} }
  97. },
  98. { /* TC_DELIVER_MODE */
  99. 0x01, 0x01,
  100. { { CTR_MS_0, MSR_P4_TC_ESCR0},
  101. { CTR_MS_2, MSR_P4_TC_ESCR1} }
  102. },
  103. { /* BPU_FETCH_REQUEST */
  104. 0x00, 0x03,
  105. { { CTR_BPU_0, MSR_P4_BPU_ESCR0},
  106. { CTR_BPU_2, MSR_P4_BPU_ESCR1} }
  107. },
  108. { /* ITLB_REFERENCE */
  109. 0x03, 0x18,
  110. { { CTR_BPU_0, MSR_P4_ITLB_ESCR0},
  111. { CTR_BPU_2, MSR_P4_ITLB_ESCR1} }
  112. },
  113. { /* MEMORY_CANCEL */
  114. 0x05, 0x02,
  115. { { CTR_FLAME_0, MSR_P4_DAC_ESCR0},
  116. { CTR_FLAME_2, MSR_P4_DAC_ESCR1} }
  117. },
  118. { /* MEMORY_COMPLETE */
  119. 0x02, 0x08,
  120. { { CTR_FLAME_0, MSR_P4_SAAT_ESCR0},
  121. { CTR_FLAME_2, MSR_P4_SAAT_ESCR1} }
  122. },
  123. { /* LOAD_PORT_REPLAY */
  124. 0x02, 0x04,
  125. { { CTR_FLAME_0, MSR_P4_SAAT_ESCR0},
  126. { CTR_FLAME_2, MSR_P4_SAAT_ESCR1} }
  127. },
  128. { /* STORE_PORT_REPLAY */
  129. 0x02, 0x05,
  130. { { CTR_FLAME_0, MSR_P4_SAAT_ESCR0},
  131. { CTR_FLAME_2, MSR_P4_SAAT_ESCR1} }
  132. },
  133. { /* MOB_LOAD_REPLAY */
  134. 0x02, 0x03,
  135. { { CTR_BPU_0, MSR_P4_MOB_ESCR0},
  136. { CTR_BPU_2, MSR_P4_MOB_ESCR1} }
  137. },
  138. { /* PAGE_WALK_TYPE */
  139. 0x04, 0x01,
  140. { { CTR_BPU_0, MSR_P4_PMH_ESCR0},
  141. { CTR_BPU_2, MSR_P4_PMH_ESCR1} }
  142. },
  143. { /* BSQ_CACHE_REFERENCE */
  144. 0x07, 0x0c,
  145. { { CTR_BPU_0, MSR_P4_BSU_ESCR0},
  146. { CTR_BPU_2, MSR_P4_BSU_ESCR1} }
  147. },
  148. { /* IOQ_ALLOCATION */
  149. 0x06, 0x03,
  150. { { CTR_BPU_0, MSR_P4_FSB_ESCR0},
  151. { 0, 0 } }
  152. },
  153. { /* IOQ_ACTIVE_ENTRIES */
  154. 0x06, 0x1a,
  155. { { CTR_BPU_2, MSR_P4_FSB_ESCR1},
  156. { 0, 0 } }
  157. },
  158. { /* FSB_DATA_ACTIVITY */
  159. 0x06, 0x17,
  160. { { CTR_BPU_0, MSR_P4_FSB_ESCR0},
  161. { CTR_BPU_2, MSR_P4_FSB_ESCR1} }
  162. },
  163. { /* BSQ_ALLOCATION */
  164. 0x07, 0x05,
  165. { { CTR_BPU_0, MSR_P4_BSU_ESCR0},
  166. { 0, 0 } }
  167. },
  168. { /* BSQ_ACTIVE_ENTRIES */
  169. 0x07, 0x06,
  170. { { CTR_BPU_2, MSR_P4_BSU_ESCR1 /* guess */},
  171. { 0, 0 } }
  172. },
  173. { /* X87_ASSIST */
  174. 0x05, 0x03,
  175. { { CTR_IQ_4, MSR_P4_CRU_ESCR2},
  176. { CTR_IQ_5, MSR_P4_CRU_ESCR3} }
  177. },
  178. { /* SSE_INPUT_ASSIST */
  179. 0x01, 0x34,
  180. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  181. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  182. },
  183. { /* PACKED_SP_UOP */
  184. 0x01, 0x08,
  185. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  186. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  187. },
  188. { /* PACKED_DP_UOP */
  189. 0x01, 0x0c,
  190. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  191. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  192. },
  193. { /* SCALAR_SP_UOP */
  194. 0x01, 0x0a,
  195. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  196. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  197. },
  198. { /* SCALAR_DP_UOP */
  199. 0x01, 0x0e,
  200. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  201. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  202. },
  203. { /* 64BIT_MMX_UOP */
  204. 0x01, 0x02,
  205. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  206. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  207. },
  208. { /* 128BIT_MMX_UOP */
  209. 0x01, 0x1a,
  210. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  211. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  212. },
  213. { /* X87_FP_UOP */
  214. 0x01, 0x04,
  215. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  216. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  217. },
  218. { /* X87_SIMD_MOVES_UOP */
  219. 0x01, 0x2e,
  220. { { CTR_FLAME_0, MSR_P4_FIRM_ESCR0},
  221. { CTR_FLAME_2, MSR_P4_FIRM_ESCR1} }
  222. },
  223. { /* MACHINE_CLEAR */
  224. 0x05, 0x02,
  225. { { CTR_IQ_4, MSR_P4_CRU_ESCR2},
  226. { CTR_IQ_5, MSR_P4_CRU_ESCR3} }
  227. },
  228. { /* GLOBAL_POWER_EVENTS */
  229. 0x06, 0x13 /* older manual says 0x05, newer 0x13 */,
  230. { { CTR_BPU_0, MSR_P4_FSB_ESCR0},
  231. { CTR_BPU_2, MSR_P4_FSB_ESCR1} }
  232. },
  233. { /* TC_MS_XFER */
  234. 0x00, 0x05,
  235. { { CTR_MS_0, MSR_P4_MS_ESCR0},
  236. { CTR_MS_2, MSR_P4_MS_ESCR1} }
  237. },
  238. { /* UOP_QUEUE_WRITES */
  239. 0x00, 0x09,
  240. { { CTR_MS_0, MSR_P4_MS_ESCR0},
  241. { CTR_MS_2, MSR_P4_MS_ESCR1} }
  242. },
  243. { /* FRONT_END_EVENT */
  244. 0x05, 0x08,
  245. { { CTR_IQ_4, MSR_P4_CRU_ESCR2},
  246. { CTR_IQ_5, MSR_P4_CRU_ESCR3} }
  247. },
  248. { /* EXECUTION_EVENT */
  249. 0x05, 0x0c,
  250. { { CTR_IQ_4, MSR_P4_CRU_ESCR2},
  251. { CTR_IQ_5, MSR_P4_CRU_ESCR3} }
  252. },
  253. { /* REPLAY_EVENT */
  254. 0x05, 0x09,
  255. { { CTR_IQ_4, MSR_P4_CRU_ESCR2},
  256. { CTR_IQ_5, MSR_P4_CRU_ESCR3} }
  257. },
  258. { /* INSTR_RETIRED */
  259. 0x04, 0x02,
  260. { { CTR_IQ_4, MSR_P4_CRU_ESCR0},
  261. { CTR_IQ_5, MSR_P4_CRU_ESCR1} }
  262. },
  263. { /* UOPS_RETIRED */
  264. 0x04, 0x01,
  265. { { CTR_IQ_4, MSR_P4_CRU_ESCR0},
  266. { CTR_IQ_5, MSR_P4_CRU_ESCR1} }
  267. },
  268. { /* UOP_TYPE */
  269. 0x02, 0x02,
  270. { { CTR_IQ_4, MSR_P4_RAT_ESCR0},
  271. { CTR_IQ_5, MSR_P4_RAT_ESCR1} }
  272. },
  273. { /* RETIRED_MISPRED_BRANCH_TYPE */
  274. 0x02, 0x05,
  275. { { CTR_MS_0, MSR_P4_TBPU_ESCR0},
  276. { CTR_MS_2, MSR_P4_TBPU_ESCR1} }
  277. },
  278. { /* RETIRED_BRANCH_TYPE */
  279. 0x02, 0x04,
  280. { { CTR_MS_0, MSR_P4_TBPU_ESCR0},
  281. { CTR_MS_2, MSR_P4_TBPU_ESCR1} }
  282. }
  283. };
  284. #define MISC_PMC_ENABLED_P(x) ((x) & 1 << 7)
  285. #define ESCR_RESERVED_BITS 0x80000003
  286. #define ESCR_CLEAR(escr) ((escr) &= ESCR_RESERVED_BITS)
  287. #define ESCR_SET_USR_0(escr, usr) ((escr) |= (((usr) & 1) << 2))
  288. #define ESCR_SET_OS_0(escr, os) ((escr) |= (((os) & 1) << 3))
  289. #define ESCR_SET_USR_1(escr, usr) ((escr) |= (((usr) & 1)))
  290. #define ESCR_SET_OS_1(escr, os) ((escr) |= (((os) & 1) << 1))
  291. #define ESCR_SET_EVENT_SELECT(escr, sel) ((escr) |= (((sel) & 0x3f) << 25))
  292. #define ESCR_SET_EVENT_MASK(escr, mask) ((escr) |= (((mask) & 0xffff) << 9))
  293. #define CCCR_RESERVED_BITS 0x38030FFF
  294. #define CCCR_CLEAR(cccr) ((cccr) &= CCCR_RESERVED_BITS)
  295. #define CCCR_SET_REQUIRED_BITS(cccr) ((cccr) |= 0x00030000)
  296. #define CCCR_SET_ESCR_SELECT(cccr, sel) ((cccr) |= (((sel) & 0x07) << 13))
  297. #define CCCR_SET_PMI_OVF_0(cccr) ((cccr) |= (1<<26))
  298. #define CCCR_SET_PMI_OVF_1(cccr) ((cccr) |= (1<<27))
  299. #define CCCR_SET_ENABLE(cccr) ((cccr) |= (1<<12))
  300. #define CCCR_SET_DISABLE(cccr) ((cccr) &= ~(1<<12))
  301. #define CCCR_OVF_P(cccr) ((cccr) & (1U<<31))
  302. #define CCCR_CLEAR_OVF(cccr) ((cccr) &= (~(1U<<31)))
  303. /* this assigns a "stagger" to the current CPU, which is used throughout
  304. the code in this module as an extra array offset, to select the "even"
  305. or "odd" part of all the divided resources. */
  306. static unsigned int get_stagger(void)
  307. {
  308. #ifdef CONFIG_SMP
  309. int cpu = smp_processor_id();
  310. return cpu != cpumask_first(this_cpu_cpumask_var_ptr(cpu_sibling_map));
  311. #endif
  312. return 0;
  313. }
  314. /* finally, mediate access to a real hardware counter
  315. by passing a "virtual" counter numer to this macro,
  316. along with your stagger setting. */
  317. #define VIRT_CTR(stagger, i) ((i) + ((num_counters) * (stagger)))
  318. static unsigned long reset_value[NUM_COUNTERS_NON_HT];
  319. static void p4_shutdown(struct op_msrs const * const msrs)
  320. {
  321. int i;
  322. for (i = 0; i < num_counters; ++i) {
  323. if (msrs->counters[i].addr)
  324. release_perfctr_nmi(msrs->counters[i].addr);
  325. }
  326. /*
  327. * some of the control registers are specially reserved in
  328. * conjunction with the counter registers (hence the starting offset).
  329. * This saves a few bits.
  330. */
  331. for (i = num_counters; i < num_controls; ++i) {
  332. if (msrs->controls[i].addr)
  333. release_evntsel_nmi(msrs->controls[i].addr);
  334. }
  335. }
  336. static int p4_fill_in_addresses(struct op_msrs * const msrs)
  337. {
  338. unsigned int i;
  339. unsigned int addr, cccraddr, stag;
  340. setup_num_counters();
  341. stag = get_stagger();
  342. /* the counter & cccr registers we pay attention to */
  343. for (i = 0; i < num_counters; ++i) {
  344. addr = p4_counters[VIRT_CTR(stag, i)].counter_address;
  345. cccraddr = p4_counters[VIRT_CTR(stag, i)].cccr_address;
  346. if (reserve_perfctr_nmi(addr)) {
  347. msrs->counters[i].addr = addr;
  348. msrs->controls[i].addr = cccraddr;
  349. }
  350. }
  351. /* 43 ESCR registers in three or four discontiguous group */
  352. for (addr = MSR_P4_BSU_ESCR0 + stag;
  353. addr < MSR_P4_IQ_ESCR0; ++i, addr += addr_increment()) {
  354. if (reserve_evntsel_nmi(addr))
  355. msrs->controls[i].addr = addr;
  356. }
  357. /* no IQ_ESCR0/1 on some models, we save a seconde time BSU_ESCR0/1
  358. * to avoid special case in nmi_{save|restore}_registers() */
  359. if (boot_cpu_data.x86_model >= 0x3) {
  360. for (addr = MSR_P4_BSU_ESCR0 + stag;
  361. addr <= MSR_P4_BSU_ESCR1; ++i, addr += addr_increment()) {
  362. if (reserve_evntsel_nmi(addr))
  363. msrs->controls[i].addr = addr;
  364. }
  365. } else {
  366. for (addr = MSR_P4_IQ_ESCR0 + stag;
  367. addr <= MSR_P4_IQ_ESCR1; ++i, addr += addr_increment()) {
  368. if (reserve_evntsel_nmi(addr))
  369. msrs->controls[i].addr = addr;
  370. }
  371. }
  372. for (addr = MSR_P4_RAT_ESCR0 + stag;
  373. addr <= MSR_P4_SSU_ESCR0; ++i, addr += addr_increment()) {
  374. if (reserve_evntsel_nmi(addr))
  375. msrs->controls[i].addr = addr;
  376. }
  377. for (addr = MSR_P4_MS_ESCR0 + stag;
  378. addr <= MSR_P4_TC_ESCR1; ++i, addr += addr_increment()) {
  379. if (reserve_evntsel_nmi(addr))
  380. msrs->controls[i].addr = addr;
  381. }
  382. for (addr = MSR_P4_IX_ESCR0 + stag;
  383. addr <= MSR_P4_CRU_ESCR3; ++i, addr += addr_increment()) {
  384. if (reserve_evntsel_nmi(addr))
  385. msrs->controls[i].addr = addr;
  386. }
  387. /* there are 2 remaining non-contiguously located ESCRs */
  388. if (num_counters == NUM_COUNTERS_NON_HT) {
  389. /* standard non-HT CPUs handle both remaining ESCRs*/
  390. if (reserve_evntsel_nmi(MSR_P4_CRU_ESCR5))
  391. msrs->controls[i++].addr = MSR_P4_CRU_ESCR5;
  392. if (reserve_evntsel_nmi(MSR_P4_CRU_ESCR4))
  393. msrs->controls[i++].addr = MSR_P4_CRU_ESCR4;
  394. } else if (stag == 0) {
  395. /* HT CPUs give the first remainder to the even thread, as
  396. the 32nd control register */
  397. if (reserve_evntsel_nmi(MSR_P4_CRU_ESCR4))
  398. msrs->controls[i++].addr = MSR_P4_CRU_ESCR4;
  399. } else {
  400. /* and two copies of the second to the odd thread,
  401. for the 22st and 23nd control registers */
  402. if (reserve_evntsel_nmi(MSR_P4_CRU_ESCR5)) {
  403. msrs->controls[i++].addr = MSR_P4_CRU_ESCR5;
  404. msrs->controls[i++].addr = MSR_P4_CRU_ESCR5;
  405. }
  406. }
  407. for (i = 0; i < num_counters; ++i) {
  408. if (!counter_config[i].enabled)
  409. continue;
  410. if (msrs->controls[i].addr)
  411. continue;
  412. op_x86_warn_reserved(i);
  413. p4_shutdown(msrs);
  414. return -EBUSY;
  415. }
  416. return 0;
  417. }
  418. static void pmc_setup_one_p4_counter(unsigned int ctr)
  419. {
  420. int i;
  421. int const maxbind = 2;
  422. unsigned int cccr = 0;
  423. unsigned int escr = 0;
  424. unsigned int high = 0;
  425. unsigned int counter_bit;
  426. struct p4_event_binding *ev = NULL;
  427. unsigned int stag;
  428. stag = get_stagger();
  429. /* convert from counter *number* to counter *bit* */
  430. counter_bit = 1 << VIRT_CTR(stag, ctr);
  431. /* find our event binding structure. */
  432. if (counter_config[ctr].event <= 0 || counter_config[ctr].event > NUM_EVENTS) {
  433. printk(KERN_ERR
  434. "oprofile: P4 event code 0x%lx out of range\n",
  435. counter_config[ctr].event);
  436. return;
  437. }
  438. ev = &(p4_events[counter_config[ctr].event - 1]);
  439. for (i = 0; i < maxbind; i++) {
  440. if (ev->bindings[i].virt_counter & counter_bit) {
  441. /* modify ESCR */
  442. rdmsr(ev->bindings[i].escr_address, escr, high);
  443. ESCR_CLEAR(escr);
  444. if (stag == 0) {
  445. ESCR_SET_USR_0(escr, counter_config[ctr].user);
  446. ESCR_SET_OS_0(escr, counter_config[ctr].kernel);
  447. } else {
  448. ESCR_SET_USR_1(escr, counter_config[ctr].user);
  449. ESCR_SET_OS_1(escr, counter_config[ctr].kernel);
  450. }
  451. ESCR_SET_EVENT_SELECT(escr, ev->event_select);
  452. ESCR_SET_EVENT_MASK(escr, counter_config[ctr].unit_mask);
  453. wrmsr(ev->bindings[i].escr_address, escr, high);
  454. /* modify CCCR */
  455. rdmsr(p4_counters[VIRT_CTR(stag, ctr)].cccr_address,
  456. cccr, high);
  457. CCCR_CLEAR(cccr);
  458. CCCR_SET_REQUIRED_BITS(cccr);
  459. CCCR_SET_ESCR_SELECT(cccr, ev->escr_select);
  460. if (stag == 0)
  461. CCCR_SET_PMI_OVF_0(cccr);
  462. else
  463. CCCR_SET_PMI_OVF_1(cccr);
  464. wrmsr(p4_counters[VIRT_CTR(stag, ctr)].cccr_address,
  465. cccr, high);
  466. return;
  467. }
  468. }
  469. printk(KERN_ERR
  470. "oprofile: P4 event code 0x%lx no binding, stag %d ctr %d\n",
  471. counter_config[ctr].event, stag, ctr);
  472. }
  473. static void p4_setup_ctrs(struct op_x86_model_spec const *model,
  474. struct op_msrs const * const msrs)
  475. {
  476. unsigned int i;
  477. unsigned int low, high;
  478. unsigned int stag;
  479. stag = get_stagger();
  480. rdmsr(MSR_IA32_MISC_ENABLE, low, high);
  481. if (!MISC_PMC_ENABLED_P(low)) {
  482. printk(KERN_ERR "oprofile: P4 PMC not available\n");
  483. return;
  484. }
  485. /* clear the cccrs we will use */
  486. for (i = 0; i < num_counters; i++) {
  487. if (unlikely(!msrs->controls[i].addr))
  488. continue;
  489. rdmsr(p4_counters[VIRT_CTR(stag, i)].cccr_address, low, high);
  490. CCCR_CLEAR(low);
  491. CCCR_SET_REQUIRED_BITS(low);
  492. wrmsr(p4_counters[VIRT_CTR(stag, i)].cccr_address, low, high);
  493. }
  494. /* clear all escrs (including those outside our concern) */
  495. for (i = num_counters; i < num_controls; i++) {
  496. if (unlikely(!msrs->controls[i].addr))
  497. continue;
  498. wrmsr(msrs->controls[i].addr, 0, 0);
  499. }
  500. /* setup all counters */
  501. for (i = 0; i < num_counters; ++i) {
  502. if (counter_config[i].enabled && msrs->controls[i].addr) {
  503. reset_value[i] = counter_config[i].count;
  504. pmc_setup_one_p4_counter(i);
  505. wrmsrl(p4_counters[VIRT_CTR(stag, i)].counter_address,
  506. -(u64)counter_config[i].count);
  507. } else {
  508. reset_value[i] = 0;
  509. }
  510. }
  511. }
  512. static int p4_check_ctrs(struct pt_regs * const regs,
  513. struct op_msrs const * const msrs)
  514. {
  515. unsigned long ctr, low, high, stag, real;
  516. int i;
  517. stag = get_stagger();
  518. for (i = 0; i < num_counters; ++i) {
  519. if (!reset_value[i])
  520. continue;
  521. /*
  522. * there is some eccentricity in the hardware which
  523. * requires that we perform 2 extra corrections:
  524. *
  525. * - check both the CCCR:OVF flag for overflow and the
  526. * counter high bit for un-flagged overflows.
  527. *
  528. * - write the counter back twice to ensure it gets
  529. * updated properly.
  530. *
  531. * the former seems to be related to extra NMIs happening
  532. * during the current NMI; the latter is reported as errata
  533. * N15 in intel doc 249199-029, pentium 4 specification
  534. * update, though their suggested work-around does not
  535. * appear to solve the problem.
  536. */
  537. real = VIRT_CTR(stag, i);
  538. rdmsr(p4_counters[real].cccr_address, low, high);
  539. rdmsr(p4_counters[real].counter_address, ctr, high);
  540. if (CCCR_OVF_P(low) || !(ctr & OP_CTR_OVERFLOW)) {
  541. oprofile_add_sample(regs, i);
  542. wrmsrl(p4_counters[real].counter_address,
  543. -(u64)reset_value[i]);
  544. CCCR_CLEAR_OVF(low);
  545. wrmsr(p4_counters[real].cccr_address, low, high);
  546. wrmsrl(p4_counters[real].counter_address,
  547. -(u64)reset_value[i]);
  548. }
  549. }
  550. /* P4 quirk: you have to re-unmask the apic vector */
  551. apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
  552. /* See op_model_ppro.c */
  553. return 1;
  554. }
  555. static void p4_start(struct op_msrs const * const msrs)
  556. {
  557. unsigned int low, high, stag;
  558. int i;
  559. stag = get_stagger();
  560. for (i = 0; i < num_counters; ++i) {
  561. if (!reset_value[i])
  562. continue;
  563. rdmsr(p4_counters[VIRT_CTR(stag, i)].cccr_address, low, high);
  564. CCCR_SET_ENABLE(low);
  565. wrmsr(p4_counters[VIRT_CTR(stag, i)].cccr_address, low, high);
  566. }
  567. }
  568. static void p4_stop(struct op_msrs const * const msrs)
  569. {
  570. unsigned int low, high, stag;
  571. int i;
  572. stag = get_stagger();
  573. for (i = 0; i < num_counters; ++i) {
  574. if (!reset_value[i])
  575. continue;
  576. rdmsr(p4_counters[VIRT_CTR(stag, i)].cccr_address, low, high);
  577. CCCR_SET_DISABLE(low);
  578. wrmsr(p4_counters[VIRT_CTR(stag, i)].cccr_address, low, high);
  579. }
  580. }
  581. #ifdef CONFIG_SMP
  582. struct op_x86_model_spec op_p4_ht2_spec = {
  583. .num_counters = NUM_COUNTERS_HT2,
  584. .num_controls = NUM_CONTROLS_HT2,
  585. .fill_in_addresses = &p4_fill_in_addresses,
  586. .setup_ctrs = &p4_setup_ctrs,
  587. .check_ctrs = &p4_check_ctrs,
  588. .start = &p4_start,
  589. .stop = &p4_stop,
  590. .shutdown = &p4_shutdown
  591. };
  592. #endif
  593. struct op_x86_model_spec op_p4_spec = {
  594. .num_counters = NUM_COUNTERS_NON_HT,
  595. .num_controls = NUM_CONTROLS_NON_HT,
  596. .fill_in_addresses = &p4_fill_in_addresses,
  597. .setup_ctrs = &p4_setup_ctrs,
  598. .check_ctrs = &p4_check_ctrs,
  599. .start = &p4_start,
  600. .stop = &p4_stop,
  601. .shutdown = &p4_shutdown
  602. };