op_model_amd.c 11 KB

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