op_model_athlon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * @file op_model_athlon.c
  3. * athlon / K7 / K8 / Family 10h model-specific MSR operations
  4. *
  5. * @remark Copyright 2002-2008 OProfile authors
  6. * @remark Read the file COPYING
  7. *
  8. * @author John Levon
  9. * @author Philippe Elie
  10. * @author Graydon Hoare
  11. * @author Robert Richter <robert.richter@amd.com>
  12. * @author Barry Kasindorf
  13. */
  14. #include <linux/oprofile.h>
  15. #include <linux/device.h>
  16. #include <linux/pci.h>
  17. #include <asm/ptrace.h>
  18. #include <asm/msr.h>
  19. #include <asm/nmi.h>
  20. #include "op_x86_model.h"
  21. #include "op_counter.h"
  22. #define NUM_COUNTERS 4
  23. #define NUM_CONTROLS 4
  24. #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
  25. #define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
  26. #define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0)
  27. #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
  28. #define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
  29. #define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
  30. #define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
  31. #define CTRL_SET_ACTIVE(n) (n |= (1<<22))
  32. #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
  33. #define CTRL_CLEAR_LO(x) (x &= (1<<21))
  34. #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
  35. #define CTRL_SET_ENABLE(val) (val |= 1<<20)
  36. #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
  37. #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
  38. #define CTRL_SET_UM(val, m) (val |= (m << 8))
  39. #define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
  40. #define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
  41. #define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
  42. #define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
  43. #define IBS_FETCH_CTL_HIGH_MASK 0xFFFFFFFF
  44. /* high dword bit IbsFetchCtl[bit 49] */
  45. #define IBS_FETCH_VALID_BIT (1UL << 17)
  46. /* high dword bit IbsFetchCtl[bit 52] */
  47. #define IBS_FETCH_PHY_ADDR_VALID_BIT (1UL << 20)
  48. /* high dword bit IbsFetchCtl[bit 48] */
  49. #define IBS_FETCH_ENABLE (1UL << 16)
  50. #define IBS_FETCH_CTL_CNT_MASK 0x00000000FFFF0000UL
  51. #define IBS_FETCH_CTL_MAX_CNT_MASK 0x000000000000FFFFUL
  52. /*IbsOpCtl masks/bits */
  53. #define IBS_OP_VALID_BIT (1ULL<<18) /* IbsOpCtl[bit18] */
  54. #define IBS_OP_ENABLE (1ULL<<17) /* IBS_OP_ENABLE[bit17]*/
  55. /* Codes used in cpu_buffer.c */
  56. #define IBS_FETCH_BEGIN 3
  57. #define IBS_OP_BEGIN 4
  58. /*IbsOpData3 masks */
  59. #define IBS_CTL_LVT_OFFSET_VALID_BIT (1ULL<<8)
  60. /*PCI Extended Configuration Constants */
  61. /* MSR to set the IBS control register APIC LVT offset */
  62. #define IBS_LVT_OFFSET_PCI 0x1CC
  63. /* The function interface needs to be fixed, something like add
  64. data. Should then be added to linux/oprofile.h. */
  65. extern void oprofile_add_ibs_sample(struct pt_regs *const regs,
  66. unsigned int * const ibs_sample, u8 code);
  67. struct ibs_fetch_sample {
  68. /* MSRC001_1031 IBS Fetch Linear Address Register */
  69. unsigned int ibs_fetch_lin_addr_low;
  70. unsigned int ibs_fetch_lin_addr_high;
  71. /* MSRC001_1030 IBS Fetch Control Register */
  72. unsigned int ibs_fetch_ctl_low;
  73. unsigned int ibs_fetch_ctl_high;
  74. /* MSRC001_1032 IBS Fetch Physical Address Register */
  75. unsigned int ibs_fetch_phys_addr_low;
  76. unsigned int ibs_fetch_phys_addr_high;
  77. };
  78. struct ibs_op_sample {
  79. /* MSRC001_1034 IBS Op Logical Address Register (IbsRIP) */
  80. unsigned int ibs_op_rip_low;
  81. unsigned int ibs_op_rip_high;
  82. /* MSRC001_1035 IBS Op Data Register */
  83. unsigned int ibs_op_data1_low;
  84. unsigned int ibs_op_data1_high;
  85. /* MSRC001_1036 IBS Op Data 2 Register */
  86. unsigned int ibs_op_data2_low;
  87. unsigned int ibs_op_data2_high;
  88. /* MSRC001_1037 IBS Op Data 3 Register */
  89. unsigned int ibs_op_data3_low;
  90. unsigned int ibs_op_data3_high;
  91. /* MSRC001_1038 IBS DC Linear Address Register (IbsDcLinAd) */
  92. unsigned int ibs_dc_linear_low;
  93. unsigned int ibs_dc_linear_high;
  94. /* MSRC001_1039 IBS DC Physical Address Register (IbsDcPhysAd) */
  95. unsigned int ibs_dc_phys_low;
  96. unsigned int ibs_dc_phys_high;
  97. };
  98. /*
  99. * unitialize the APIC for the IBS interrupts if needed on AMD Family10h+
  100. */
  101. static void clear_ibs_nmi(void);
  102. static unsigned long reset_value[NUM_COUNTERS];
  103. static int ibs_allowed; /* AMD Family10h and later */
  104. struct op_ibs_config {
  105. unsigned long op_enabled;
  106. unsigned long fetch_enabled;
  107. unsigned long max_cnt_fetch;
  108. unsigned long max_cnt_op;
  109. unsigned long rand_en;
  110. unsigned long dispatched_ops;
  111. };
  112. static struct op_ibs_config ibs_config;
  113. /* functions for op_amd_spec */
  114. static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
  115. {
  116. int i;
  117. for (i = 0; i < NUM_COUNTERS; i++) {
  118. if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  119. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  120. else
  121. msrs->counters[i].addr = 0;
  122. }
  123. for (i = 0; i < NUM_CONTROLS; i++) {
  124. if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
  125. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  126. else
  127. msrs->controls[i].addr = 0;
  128. }
  129. }
  130. static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
  131. {
  132. unsigned int low, high;
  133. int i;
  134. /* clear all counters */
  135. for (i = 0 ; i < NUM_CONTROLS; ++i) {
  136. if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
  137. continue;
  138. CTRL_READ(low, high, msrs, i);
  139. CTRL_CLEAR_LO(low);
  140. CTRL_CLEAR_HI(high);
  141. CTRL_WRITE(low, high, msrs, i);
  142. }
  143. /* avoid a false detection of ctr overflows in NMI handler */
  144. for (i = 0; i < NUM_COUNTERS; ++i) {
  145. if (unlikely(!CTR_IS_RESERVED(msrs, i)))
  146. continue;
  147. CTR_WRITE(1, msrs, i);
  148. }
  149. /* enable active counters */
  150. for (i = 0; i < NUM_COUNTERS; ++i) {
  151. if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
  152. reset_value[i] = counter_config[i].count;
  153. CTR_WRITE(counter_config[i].count, msrs, i);
  154. CTRL_READ(low, high, msrs, i);
  155. CTRL_CLEAR_LO(low);
  156. CTRL_CLEAR_HI(high);
  157. CTRL_SET_ENABLE(low);
  158. CTRL_SET_USR(low, counter_config[i].user);
  159. CTRL_SET_KERN(low, counter_config[i].kernel);
  160. CTRL_SET_UM(low, counter_config[i].unit_mask);
  161. CTRL_SET_EVENT_LOW(low, counter_config[i].event);
  162. CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
  163. CTRL_SET_HOST_ONLY(high, 0);
  164. CTRL_SET_GUEST_ONLY(high, 0);
  165. CTRL_WRITE(low, high, msrs, i);
  166. } else {
  167. reset_value[i] = 0;
  168. }
  169. }
  170. }
  171. static inline int
  172. op_amd_handle_ibs(struct pt_regs * const regs,
  173. struct op_msrs const * const msrs)
  174. {
  175. unsigned int low, high;
  176. struct ibs_fetch_sample ibs_fetch;
  177. struct ibs_op_sample ibs_op;
  178. if (!ibs_allowed)
  179. return 1;
  180. if (ibs_config.fetch_enabled) {
  181. rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
  182. if (high & IBS_FETCH_VALID_BIT) {
  183. ibs_fetch.ibs_fetch_ctl_high = high;
  184. ibs_fetch.ibs_fetch_ctl_low = low;
  185. rdmsr(MSR_AMD64_IBSFETCHLINAD, low, high);
  186. ibs_fetch.ibs_fetch_lin_addr_high = high;
  187. ibs_fetch.ibs_fetch_lin_addr_low = low;
  188. rdmsr(MSR_AMD64_IBSFETCHPHYSAD, low, high);
  189. ibs_fetch.ibs_fetch_phys_addr_high = high;
  190. ibs_fetch.ibs_fetch_phys_addr_low = low;
  191. oprofile_add_ibs_sample(regs,
  192. (unsigned int *)&ibs_fetch,
  193. IBS_FETCH_BEGIN);
  194. /*reenable the IRQ */
  195. rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
  196. high &= ~(IBS_FETCH_VALID_BIT);
  197. high |= IBS_FETCH_ENABLE;
  198. low &= IBS_FETCH_CTL_MAX_CNT_MASK;
  199. wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
  200. }
  201. }
  202. if (ibs_config.op_enabled) {
  203. rdmsr(MSR_AMD64_IBSOPCTL, low, high);
  204. if (low & IBS_OP_VALID_BIT) {
  205. rdmsr(MSR_AMD64_IBSOPRIP, low, high);
  206. ibs_op.ibs_op_rip_low = low;
  207. ibs_op.ibs_op_rip_high = high;
  208. rdmsr(MSR_AMD64_IBSOPDATA, low, high);
  209. ibs_op.ibs_op_data1_low = low;
  210. ibs_op.ibs_op_data1_high = high;
  211. rdmsr(MSR_AMD64_IBSOPDATA2, low, high);
  212. ibs_op.ibs_op_data2_low = low;
  213. ibs_op.ibs_op_data2_high = high;
  214. rdmsr(MSR_AMD64_IBSOPDATA3, low, high);
  215. ibs_op.ibs_op_data3_low = low;
  216. ibs_op.ibs_op_data3_high = high;
  217. rdmsr(MSR_AMD64_IBSDCLINAD, low, high);
  218. ibs_op.ibs_dc_linear_low = low;
  219. ibs_op.ibs_dc_linear_high = high;
  220. rdmsr(MSR_AMD64_IBSDCPHYSAD, low, high);
  221. ibs_op.ibs_dc_phys_low = low;
  222. ibs_op.ibs_dc_phys_high = high;
  223. /* reenable the IRQ */
  224. oprofile_add_ibs_sample(regs,
  225. (unsigned int *)&ibs_op,
  226. IBS_OP_BEGIN);
  227. rdmsr(MSR_AMD64_IBSOPCTL, low, high);
  228. low &= ~(IBS_OP_VALID_BIT);
  229. low |= IBS_OP_ENABLE;
  230. wrmsr(MSR_AMD64_IBSOPCTL, low, high);
  231. }
  232. }
  233. return 1;
  234. }
  235. static int op_amd_check_ctrs(struct pt_regs * const regs,
  236. struct op_msrs const * const msrs)
  237. {
  238. unsigned int low, high;
  239. int i;
  240. for (i = 0 ; i < NUM_COUNTERS; ++i) {
  241. if (!reset_value[i])
  242. continue;
  243. CTR_READ(low, high, msrs, i);
  244. if (CTR_OVERFLOWED(low)) {
  245. oprofile_add_sample(regs, i);
  246. CTR_WRITE(reset_value[i], msrs, i);
  247. }
  248. }
  249. op_amd_handle_ibs(regs, msrs);
  250. /* See op_model_ppro.c */
  251. return 1;
  252. }
  253. static void op_amd_start(struct op_msrs const * const msrs)
  254. {
  255. unsigned int low, high;
  256. int i;
  257. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  258. if (reset_value[i]) {
  259. CTRL_READ(low, high, msrs, i);
  260. CTRL_SET_ACTIVE(low);
  261. CTRL_WRITE(low, high, msrs, i);
  262. }
  263. }
  264. if (ibs_allowed && ibs_config.fetch_enabled) {
  265. low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
  266. high = IBS_FETCH_ENABLE;
  267. wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
  268. }
  269. if (ibs_allowed && ibs_config.op_enabled) {
  270. low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF) + IBS_OP_ENABLE;
  271. high = 0;
  272. wrmsr(MSR_AMD64_IBSOPCTL, low, high);
  273. }
  274. }
  275. static void op_amd_stop(struct op_msrs const * const msrs)
  276. {
  277. unsigned int low, high;
  278. int i;
  279. /* Subtle: stop on all counters to avoid race with
  280. * setting our pm callback */
  281. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  282. if (!reset_value[i])
  283. continue;
  284. CTRL_READ(low, high, msrs, i);
  285. CTRL_SET_INACTIVE(low);
  286. CTRL_WRITE(low, high, msrs, i);
  287. }
  288. if (ibs_allowed && ibs_config.fetch_enabled) {
  289. low = 0; /* clear max count and enable */
  290. high = 0;
  291. wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
  292. }
  293. if (ibs_allowed && ibs_config.op_enabled) {
  294. low = 0; /* clear max count and enable */
  295. high = 0;
  296. wrmsr(MSR_AMD64_IBSOPCTL, low, high);
  297. }
  298. }
  299. static void op_amd_shutdown(struct op_msrs const * const msrs)
  300. {
  301. int i;
  302. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  303. if (CTR_IS_RESERVED(msrs, i))
  304. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  305. }
  306. for (i = 0 ; i < NUM_CONTROLS ; ++i) {
  307. if (CTRL_IS_RESERVED(msrs, i))
  308. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  309. }
  310. }
  311. static u8 ibs_eilvt_off;
  312. static inline void apic_init_ibs_nmi_per_cpu(void *arg)
  313. {
  314. ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
  315. }
  316. static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
  317. {
  318. setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
  319. }
  320. static int pfm_amd64_setup_eilvt(void)
  321. {
  322. #define IBSCTL_LVTOFFSETVAL (1 << 8)
  323. #define IBSCTL 0x1cc
  324. struct pci_dev *cpu_cfg;
  325. int nodes;
  326. u32 value = 0;
  327. /* per CPU setup */
  328. on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
  329. nodes = 0;
  330. cpu_cfg = NULL;
  331. do {
  332. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  333. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  334. cpu_cfg);
  335. if (!cpu_cfg)
  336. break;
  337. ++nodes;
  338. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  339. | IBSCTL_LVTOFFSETVAL);
  340. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  341. if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
  342. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  343. "IBSCTL = 0x%08x", value);
  344. return 1;
  345. }
  346. } while (1);
  347. if (!nodes) {
  348. printk(KERN_DEBUG "No CPU node configured for IBS");
  349. return 1;
  350. }
  351. #ifdef CONFIG_NUMA
  352. /* Sanity check */
  353. /* Works only for 64bit with proper numa implementation. */
  354. if (nodes != num_possible_nodes()) {
  355. printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
  356. "found: %d, expected %d",
  357. nodes, num_possible_nodes());
  358. return 1;
  359. }
  360. #endif
  361. return 0;
  362. }
  363. /*
  364. * initialize the APIC for the IBS interrupts
  365. * if available (AMD Family10h rev B0 and later)
  366. */
  367. static void setup_ibs(void)
  368. {
  369. ibs_allowed = boot_cpu_has(X86_FEATURE_IBS);
  370. if (!ibs_allowed)
  371. return;
  372. if (pfm_amd64_setup_eilvt())
  373. ibs_allowed = 0;
  374. }
  375. /*
  376. * unitialize the APIC for the IBS interrupts if needed on AMD Family10h
  377. * rev B0 and later */
  378. static void clear_ibs_nmi(void)
  379. {
  380. if (ibs_allowed)
  381. on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
  382. }
  383. static int (*create_arch_files)(struct super_block * sb, struct dentry * root);
  384. static int setup_ibs_files(struct super_block * sb, struct dentry * root)
  385. {
  386. char buf[12];
  387. struct dentry *dir;
  388. int ret = 0;
  389. /* architecture specific files */
  390. if (create_arch_files)
  391. ret = create_arch_files(sb, root);
  392. if (ret)
  393. return ret;
  394. if (!ibs_allowed)
  395. return ret;
  396. /* model specific files */
  397. /* setup some reasonable defaults */
  398. ibs_config.max_cnt_fetch = 250000;
  399. ibs_config.fetch_enabled = 0;
  400. ibs_config.max_cnt_op = 250000;
  401. ibs_config.op_enabled = 0;
  402. ibs_config.dispatched_ops = 1;
  403. snprintf(buf, sizeof(buf), "ibs_fetch");
  404. dir = oprofilefs_mkdir(sb, root, buf);
  405. oprofilefs_create_ulong(sb, dir, "rand_enable",
  406. &ibs_config.rand_en);
  407. oprofilefs_create_ulong(sb, dir, "enable",
  408. &ibs_config.fetch_enabled);
  409. oprofilefs_create_ulong(sb, dir, "max_count",
  410. &ibs_config.max_cnt_fetch);
  411. snprintf(buf, sizeof(buf), "ibs_uops");
  412. dir = oprofilefs_mkdir(sb, root, buf);
  413. oprofilefs_create_ulong(sb, dir, "enable",
  414. &ibs_config.op_enabled);
  415. oprofilefs_create_ulong(sb, dir, "max_count",
  416. &ibs_config.max_cnt_op);
  417. oprofilefs_create_ulong(sb, dir, "dispatched_ops",
  418. &ibs_config.dispatched_ops);
  419. return 0;
  420. }
  421. static int op_amd_init(struct oprofile_operations *ops)
  422. {
  423. setup_ibs();
  424. create_arch_files = ops->create_files;
  425. ops->create_files = setup_ibs_files;
  426. return 0;
  427. }
  428. static void op_amd_exit(void)
  429. {
  430. clear_ibs_nmi();
  431. }
  432. struct op_x86_model_spec const op_amd_spec = {
  433. .init = op_amd_init,
  434. .exit = op_amd_exit,
  435. .num_counters = NUM_COUNTERS,
  436. .num_controls = NUM_CONTROLS,
  437. .fill_in_addresses = &op_amd_fill_in_addresses,
  438. .setup_ctrs = &op_amd_setup_ctrs,
  439. .check_ctrs = &op_amd_check_ctrs,
  440. .start = &op_amd_start,
  441. .stop = &op_amd_stop,
  442. .shutdown = &op_amd_shutdown
  443. };