op_model_amd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. static inline void op_amd_start_ibs(void)
  188. {
  189. unsigned int low, high;
  190. if (has_ibs && ibs_config.fetch_enabled) {
  191. low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
  192. high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
  193. + IBS_FETCH_HIGH_ENABLE;
  194. wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
  195. }
  196. if (has_ibs && ibs_config.op_enabled) {
  197. low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
  198. + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
  199. + IBS_OP_LOW_ENABLE;
  200. high = 0;
  201. wrmsr(MSR_AMD64_IBSOPCTL, low, high);
  202. }
  203. }
  204. static void op_amd_stop_ibs(void)
  205. {
  206. unsigned int low, high;
  207. if (has_ibs && ibs_config.fetch_enabled) {
  208. /* clear max count and enable */
  209. low = 0;
  210. high = 0;
  211. wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
  212. }
  213. if (has_ibs && ibs_config.op_enabled) {
  214. /* clear max count and enable */
  215. low = 0;
  216. high = 0;
  217. wrmsr(MSR_AMD64_IBSOPCTL, low, high);
  218. }
  219. }
  220. #else
  221. static inline int op_amd_handle_ibs(struct pt_regs * const regs,
  222. struct op_msrs const * const msrs) { }
  223. static inline void op_amd_start_ibs(void) { }
  224. static inline void op_amd_stop_ibs(void) { }
  225. #endif
  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. op_amd_start_ibs();
  256. }
  257. static void op_amd_stop(struct op_msrs const * const msrs)
  258. {
  259. unsigned int low, high;
  260. int i;
  261. /*
  262. * Subtle: stop on all counters to avoid race with setting our
  263. * pm callback
  264. */
  265. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  266. if (!reset_value[i])
  267. continue;
  268. CTRL_READ(low, high, msrs, i);
  269. CTRL_SET_INACTIVE(low);
  270. CTRL_WRITE(low, high, msrs, i);
  271. }
  272. op_amd_stop_ibs();
  273. }
  274. static void op_amd_shutdown(struct op_msrs const * const msrs)
  275. {
  276. int i;
  277. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  278. if (CTR_IS_RESERVED(msrs, i))
  279. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  280. }
  281. for (i = 0 ; i < NUM_CONTROLS ; ++i) {
  282. if (CTRL_IS_RESERVED(msrs, i))
  283. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  284. }
  285. }
  286. #ifdef CONFIG_OPROFILE_IBS
  287. static u8 ibs_eilvt_off;
  288. static inline void apic_init_ibs_nmi_per_cpu(void *arg)
  289. {
  290. ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
  291. }
  292. static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
  293. {
  294. setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
  295. }
  296. static int init_ibs_nmi(void)
  297. {
  298. #define IBSCTL_LVTOFFSETVAL (1 << 8)
  299. #define IBSCTL 0x1cc
  300. struct pci_dev *cpu_cfg;
  301. int nodes;
  302. u32 value = 0;
  303. /* per CPU setup */
  304. on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
  305. nodes = 0;
  306. cpu_cfg = NULL;
  307. do {
  308. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  309. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  310. cpu_cfg);
  311. if (!cpu_cfg)
  312. break;
  313. ++nodes;
  314. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  315. | IBSCTL_LVTOFFSETVAL);
  316. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  317. if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
  318. pci_dev_put(cpu_cfg);
  319. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  320. "IBSCTL = 0x%08x", value);
  321. return 1;
  322. }
  323. } while (1);
  324. if (!nodes) {
  325. printk(KERN_DEBUG "No CPU node configured for IBS");
  326. return 1;
  327. }
  328. #ifdef CONFIG_NUMA
  329. /* Sanity check */
  330. /* Works only for 64bit with proper numa implementation. */
  331. if (nodes != num_possible_nodes()) {
  332. printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
  333. "found: %d, expected %d",
  334. nodes, num_possible_nodes());
  335. return 1;
  336. }
  337. #endif
  338. return 0;
  339. }
  340. /* uninitialize the APIC for the IBS interrupts if needed */
  341. static void clear_ibs_nmi(void)
  342. {
  343. if (has_ibs)
  344. on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
  345. }
  346. /* initialize the APIC for the IBS interrupts if available */
  347. static void ibs_init(void)
  348. {
  349. has_ibs = boot_cpu_has(X86_FEATURE_IBS);
  350. if (!has_ibs)
  351. return;
  352. if (init_ibs_nmi()) {
  353. has_ibs = 0;
  354. return;
  355. }
  356. printk(KERN_INFO "oprofile: AMD IBS detected\n");
  357. }
  358. static void ibs_exit(void)
  359. {
  360. if (!has_ibs)
  361. return;
  362. clear_ibs_nmi();
  363. }
  364. static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
  365. static int setup_ibs_files(struct super_block *sb, struct dentry *root)
  366. {
  367. struct dentry *dir;
  368. int ret = 0;
  369. /* architecture specific files */
  370. if (create_arch_files)
  371. ret = create_arch_files(sb, root);
  372. if (ret)
  373. return ret;
  374. if (!has_ibs)
  375. return ret;
  376. /* model specific files */
  377. /* setup some reasonable defaults */
  378. ibs_config.max_cnt_fetch = 250000;
  379. ibs_config.fetch_enabled = 0;
  380. ibs_config.max_cnt_op = 250000;
  381. ibs_config.op_enabled = 0;
  382. ibs_config.dispatched_ops = 1;
  383. dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
  384. oprofilefs_create_ulong(sb, dir, "enable",
  385. &ibs_config.fetch_enabled);
  386. oprofilefs_create_ulong(sb, dir, "max_count",
  387. &ibs_config.max_cnt_fetch);
  388. oprofilefs_create_ulong(sb, dir, "rand_enable",
  389. &ibs_config.rand_en);
  390. dir = oprofilefs_mkdir(sb, root, "ibs_op");
  391. oprofilefs_create_ulong(sb, dir, "enable",
  392. &ibs_config.op_enabled);
  393. oprofilefs_create_ulong(sb, dir, "max_count",
  394. &ibs_config.max_cnt_op);
  395. oprofilefs_create_ulong(sb, dir, "dispatched_ops",
  396. &ibs_config.dispatched_ops);
  397. return 0;
  398. }
  399. static int op_amd_init(struct oprofile_operations *ops)
  400. {
  401. ibs_init();
  402. create_arch_files = ops->create_files;
  403. ops->create_files = setup_ibs_files;
  404. return 0;
  405. }
  406. static void op_amd_exit(void)
  407. {
  408. ibs_exit();
  409. }
  410. #else
  411. /* no IBS support */
  412. static int op_amd_init(struct oprofile_operations *ops)
  413. {
  414. return 0;
  415. }
  416. static void op_amd_exit(void) {}
  417. #endif /* CONFIG_OPROFILE_IBS */
  418. struct op_x86_model_spec const op_amd_spec = {
  419. .init = op_amd_init,
  420. .exit = op_amd_exit,
  421. .num_counters = NUM_COUNTERS,
  422. .num_controls = NUM_CONTROLS,
  423. .fill_in_addresses = &op_amd_fill_in_addresses,
  424. .setup_ctrs = &op_amd_setup_ctrs,
  425. .check_ctrs = &op_amd_check_ctrs,
  426. .start = &op_amd_start,
  427. .stop = &op_amd_stop,
  428. .shutdown = &op_amd_shutdown
  429. };