op_model_amd.c 14 KB

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