op_model_amd.c 12 KB

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