op_model_amd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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_FETCH_HIGH_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_LOW_ENABLE;
  271. high = 0;
  272. wrmsr(MSR_AMD64_IBSOPCTL, low, high);
  273. }
  274. #endif
  275. }
  276. static void op_amd_stop(struct op_msrs const * const msrs)
  277. {
  278. unsigned int low, high;
  279. int i;
  280. /* Subtle: stop on all counters to avoid race with
  281. * setting our pm callback */
  282. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  283. if (!reset_value[i])
  284. continue;
  285. CTRL_READ(low, high, msrs, i);
  286. CTRL_SET_INACTIVE(low);
  287. CTRL_WRITE(low, high, msrs, i);
  288. }
  289. #ifdef CONFIG_OPROFILE_IBS
  290. if (ibs_allowed && ibs_config.fetch_enabled) {
  291. low = 0; /* clear max count and enable */
  292. high = 0;
  293. wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
  294. }
  295. if (ibs_allowed && ibs_config.op_enabled) {
  296. low = 0; /* clear max count and enable */
  297. high = 0;
  298. wrmsr(MSR_AMD64_IBSOPCTL, low, high);
  299. }
  300. #endif
  301. }
  302. static void op_amd_shutdown(struct op_msrs const * const msrs)
  303. {
  304. int i;
  305. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  306. if (CTR_IS_RESERVED(msrs, i))
  307. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  308. }
  309. for (i = 0 ; i < NUM_CONTROLS ; ++i) {
  310. if (CTRL_IS_RESERVED(msrs, i))
  311. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  312. }
  313. }
  314. #ifndef CONFIG_OPROFILE_IBS
  315. /* no IBS support */
  316. static int op_amd_init(struct oprofile_operations *ops)
  317. {
  318. return 0;
  319. }
  320. static void op_amd_exit(void) {}
  321. #else
  322. static u8 ibs_eilvt_off;
  323. static inline void apic_init_ibs_nmi_per_cpu(void *arg)
  324. {
  325. ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
  326. }
  327. static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
  328. {
  329. setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
  330. }
  331. static int pfm_amd64_setup_eilvt(void)
  332. {
  333. #define IBSCTL_LVTOFFSETVAL (1 << 8)
  334. #define IBSCTL 0x1cc
  335. struct pci_dev *cpu_cfg;
  336. int nodes;
  337. u32 value = 0;
  338. /* per CPU setup */
  339. on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
  340. nodes = 0;
  341. cpu_cfg = NULL;
  342. do {
  343. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  344. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  345. cpu_cfg);
  346. if (!cpu_cfg)
  347. break;
  348. ++nodes;
  349. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  350. | IBSCTL_LVTOFFSETVAL);
  351. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  352. if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
  353. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  354. "IBSCTL = 0x%08x", value);
  355. return 1;
  356. }
  357. } while (1);
  358. if (!nodes) {
  359. printk(KERN_DEBUG "No CPU node configured for IBS");
  360. return 1;
  361. }
  362. #ifdef CONFIG_NUMA
  363. /* Sanity check */
  364. /* Works only for 64bit with proper numa implementation. */
  365. if (nodes != num_possible_nodes()) {
  366. printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
  367. "found: %d, expected %d",
  368. nodes, num_possible_nodes());
  369. return 1;
  370. }
  371. #endif
  372. return 0;
  373. }
  374. /*
  375. * initialize the APIC for the IBS interrupts
  376. * if available (AMD Family10h rev B0 and later)
  377. */
  378. static void setup_ibs(void)
  379. {
  380. ibs_allowed = boot_cpu_has(X86_FEATURE_IBS);
  381. if (!ibs_allowed)
  382. return;
  383. if (pfm_amd64_setup_eilvt()) {
  384. ibs_allowed = 0;
  385. return;
  386. }
  387. printk(KERN_INFO "oprofile: AMD IBS detected\n");
  388. }
  389. /*
  390. * unitialize the APIC for the IBS interrupts if needed on AMD Family10h
  391. * rev B0 and later */
  392. static void clear_ibs_nmi(void)
  393. {
  394. if (ibs_allowed)
  395. on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
  396. }
  397. static int (*create_arch_files)(struct super_block * sb, struct dentry * root);
  398. static int setup_ibs_files(struct super_block * sb, struct dentry * root)
  399. {
  400. struct dentry *dir;
  401. int ret = 0;
  402. /* architecture specific files */
  403. if (create_arch_files)
  404. ret = create_arch_files(sb, root);
  405. if (ret)
  406. return ret;
  407. if (!ibs_allowed)
  408. return ret;
  409. /* model specific files */
  410. /* setup some reasonable defaults */
  411. ibs_config.max_cnt_fetch = 250000;
  412. ibs_config.fetch_enabled = 0;
  413. ibs_config.max_cnt_op = 250000;
  414. ibs_config.op_enabled = 0;
  415. ibs_config.dispatched_ops = 1;
  416. dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
  417. oprofilefs_create_ulong(sb, dir, "enable",
  418. &ibs_config.fetch_enabled);
  419. oprofilefs_create_ulong(sb, dir, "max_count",
  420. &ibs_config.max_cnt_fetch);
  421. oprofilefs_create_ulong(sb, dir, "rand_enable",
  422. &ibs_config.rand_en);
  423. dir = oprofilefs_mkdir(sb, root, "ibs_uops");
  424. oprofilefs_create_ulong(sb, dir, "enable",
  425. &ibs_config.op_enabled);
  426. oprofilefs_create_ulong(sb, dir, "max_count",
  427. &ibs_config.max_cnt_op);
  428. oprofilefs_create_ulong(sb, dir, "dispatched_ops",
  429. &ibs_config.dispatched_ops);
  430. return 0;
  431. }
  432. static int op_amd_init(struct oprofile_operations *ops)
  433. {
  434. setup_ibs();
  435. create_arch_files = ops->create_files;
  436. ops->create_files = setup_ibs_files;
  437. return 0;
  438. }
  439. static void op_amd_exit(void)
  440. {
  441. clear_ibs_nmi();
  442. }
  443. #endif
  444. struct op_x86_model_spec const op_amd_spec = {
  445. .init = op_amd_init,
  446. .exit = op_amd_exit,
  447. .num_counters = NUM_COUNTERS,
  448. .num_controls = NUM_CONTROLS,
  449. .fill_in_addresses = &op_amd_fill_in_addresses,
  450. .setup_ctrs = &op_amd_setup_ctrs,
  451. .check_ctrs = &op_amd_check_ctrs,
  452. .start = &op_amd_start,
  453. .stop = &op_amd_stop,
  454. .shutdown = &op_amd_shutdown
  455. };