op_model_amd.c 12 KB

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