op_model_amd.c 12 KB

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