op_model_athlon.c 13 KB

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