op_model_amd.c 14 KB

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