op_model_amd.c 12 KB

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