op_model_amd.c 12 KB

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