op_model_amd.c 11 KB

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