op_model_amd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 = i % NUM_COUNTERS;
  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. }
  123. /* clear all counters */
  124. for (i = 0; i < NUM_CONTROLS; ++i) {
  125. if (unlikely(!msrs->controls[i].addr))
  126. continue;
  127. rdmsrl(msrs->controls[i].addr, val);
  128. val &= model->reserved;
  129. wrmsrl(msrs->controls[i].addr, val);
  130. }
  131. /* avoid a false detection of ctr overflows in NMI handler */
  132. for (i = 0; i < NUM_COUNTERS; ++i) {
  133. if (unlikely(!msrs->counters[i].addr))
  134. continue;
  135. wrmsrl(msrs->counters[i].addr, -1LL);
  136. }
  137. /* enable active counters */
  138. for (i = 0; i < NUM_COUNTERS; ++i) {
  139. int virt = op_x86_phys_to_virt(i);
  140. if (!counter_config[virt].enabled)
  141. continue;
  142. if (!msrs->counters[i].addr)
  143. continue;
  144. /* setup counter registers */
  145. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  146. /* setup control registers */
  147. rdmsrl(msrs->controls[i].addr, val);
  148. val &= model->reserved;
  149. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  150. wrmsrl(msrs->controls[i].addr, val);
  151. }
  152. }
  153. #ifdef CONFIG_OPROFILE_IBS
  154. static inline int
  155. op_amd_handle_ibs(struct pt_regs * const regs,
  156. struct op_msrs const * const msrs)
  157. {
  158. u64 val, ctl;
  159. struct op_entry entry;
  160. if (!has_ibs)
  161. return 0;
  162. if (ibs_config.fetch_enabled) {
  163. rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
  164. if (ctl & IBS_FETCH_VAL) {
  165. rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
  166. oprofile_write_reserve(&entry, regs, val,
  167. IBS_FETCH_CODE, IBS_FETCH_SIZE);
  168. oprofile_add_data64(&entry, val);
  169. oprofile_add_data64(&entry, ctl);
  170. rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
  171. oprofile_add_data64(&entry, val);
  172. oprofile_write_commit(&entry);
  173. /* reenable the IRQ */
  174. ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT_MASK);
  175. ctl |= IBS_FETCH_ENABLE;
  176. wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
  177. }
  178. }
  179. if (ibs_config.op_enabled) {
  180. rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
  181. if (ctl & IBS_OP_VAL) {
  182. rdmsrl(MSR_AMD64_IBSOPRIP, val);
  183. oprofile_write_reserve(&entry, regs, val,
  184. IBS_OP_CODE, IBS_OP_SIZE);
  185. oprofile_add_data64(&entry, val);
  186. rdmsrl(MSR_AMD64_IBSOPDATA, val);
  187. oprofile_add_data64(&entry, val);
  188. rdmsrl(MSR_AMD64_IBSOPDATA2, val);
  189. oprofile_add_data64(&entry, val);
  190. rdmsrl(MSR_AMD64_IBSOPDATA3, val);
  191. oprofile_add_data64(&entry, val);
  192. rdmsrl(MSR_AMD64_IBSDCLINAD, val);
  193. oprofile_add_data64(&entry, val);
  194. rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
  195. oprofile_add_data64(&entry, val);
  196. oprofile_write_commit(&entry);
  197. /* reenable the IRQ */
  198. ctl &= ~IBS_OP_VAL & 0xFFFFFFFF;
  199. ctl |= IBS_OP_ENABLE;
  200. wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
  201. }
  202. }
  203. return 1;
  204. }
  205. static inline void op_amd_start_ibs(void)
  206. {
  207. u64 val;
  208. if (has_ibs && ibs_config.fetch_enabled) {
  209. val = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
  210. val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
  211. val |= IBS_FETCH_ENABLE;
  212. wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
  213. }
  214. if (has_ibs && ibs_config.op_enabled) {
  215. val = (ibs_config.max_cnt_op >> 4) & 0xFFFF;
  216. val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
  217. val |= IBS_OP_ENABLE;
  218. wrmsrl(MSR_AMD64_IBSOPCTL, val);
  219. }
  220. }
  221. static void op_amd_stop_ibs(void)
  222. {
  223. if (has_ibs && ibs_config.fetch_enabled)
  224. /* clear max count and enable */
  225. wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
  226. if (has_ibs && ibs_config.op_enabled)
  227. /* clear max count and enable */
  228. wrmsrl(MSR_AMD64_IBSOPCTL, 0);
  229. }
  230. #else
  231. static inline int op_amd_handle_ibs(struct pt_regs * const regs,
  232. struct op_msrs const * const msrs)
  233. {
  234. return 0;
  235. }
  236. static inline void op_amd_start_ibs(void) { }
  237. static inline void op_amd_stop_ibs(void) { }
  238. #endif
  239. static int op_amd_check_ctrs(struct pt_regs * const regs,
  240. struct op_msrs const * const msrs)
  241. {
  242. u64 val;
  243. int i;
  244. for (i = 0; i < NUM_COUNTERS; ++i) {
  245. int virt = op_x86_phys_to_virt(i);
  246. if (!reset_value[virt])
  247. continue;
  248. rdmsrl(msrs->counters[i].addr, val);
  249. /* bit is clear if overflowed: */
  250. if (val & OP_CTR_OVERFLOW)
  251. continue;
  252. oprofile_add_sample(regs, virt);
  253. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  254. }
  255. op_amd_handle_ibs(regs, msrs);
  256. /* See op_model_ppro.c */
  257. return 1;
  258. }
  259. static void op_amd_start(struct op_msrs const * const msrs)
  260. {
  261. u64 val;
  262. int i;
  263. for (i = 0; i < NUM_COUNTERS; ++i) {
  264. if (!reset_value[op_x86_phys_to_virt(i)])
  265. continue;
  266. rdmsrl(msrs->controls[i].addr, val);
  267. val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  268. wrmsrl(msrs->controls[i].addr, val);
  269. }
  270. op_amd_start_ibs();
  271. }
  272. static void op_amd_stop(struct op_msrs const * const msrs)
  273. {
  274. u64 val;
  275. int i;
  276. /*
  277. * Subtle: stop on all counters to avoid race with setting our
  278. * pm callback
  279. */
  280. for (i = 0; i < NUM_COUNTERS; ++i) {
  281. if (!reset_value[op_x86_phys_to_virt(i)])
  282. continue;
  283. rdmsrl(msrs->controls[i].addr, val);
  284. val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
  285. wrmsrl(msrs->controls[i].addr, val);
  286. }
  287. op_amd_stop_ibs();
  288. }
  289. static void op_amd_shutdown(struct op_msrs const * const msrs)
  290. {
  291. int i;
  292. for (i = 0; i < NUM_COUNTERS; ++i) {
  293. if (msrs->counters[i].addr)
  294. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  295. }
  296. for (i = 0; i < NUM_CONTROLS; ++i) {
  297. if (msrs->controls[i].addr)
  298. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  299. }
  300. }
  301. #ifdef CONFIG_OPROFILE_IBS
  302. static u8 ibs_eilvt_off;
  303. static inline void apic_init_ibs_nmi_per_cpu(void *arg)
  304. {
  305. ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
  306. }
  307. static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
  308. {
  309. setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
  310. }
  311. static int init_ibs_nmi(void)
  312. {
  313. #define IBSCTL_LVTOFFSETVAL (1 << 8)
  314. #define IBSCTL 0x1cc
  315. struct pci_dev *cpu_cfg;
  316. int nodes;
  317. u32 value = 0;
  318. /* per CPU setup */
  319. on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
  320. nodes = 0;
  321. cpu_cfg = NULL;
  322. do {
  323. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  324. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  325. cpu_cfg);
  326. if (!cpu_cfg)
  327. break;
  328. ++nodes;
  329. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  330. | IBSCTL_LVTOFFSETVAL);
  331. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  332. if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
  333. pci_dev_put(cpu_cfg);
  334. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  335. "IBSCTL = 0x%08x", value);
  336. return 1;
  337. }
  338. } while (1);
  339. if (!nodes) {
  340. printk(KERN_DEBUG "No CPU node configured for IBS");
  341. return 1;
  342. }
  343. #ifdef CONFIG_NUMA
  344. /* Sanity check */
  345. /* Works only for 64bit with proper numa implementation. */
  346. if (nodes != num_possible_nodes()) {
  347. printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
  348. "found: %d, expected %d",
  349. nodes, num_possible_nodes());
  350. return 1;
  351. }
  352. #endif
  353. return 0;
  354. }
  355. /* uninitialize the APIC for the IBS interrupts if needed */
  356. static void clear_ibs_nmi(void)
  357. {
  358. if (has_ibs)
  359. on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
  360. }
  361. /* initialize the APIC for the IBS interrupts if available */
  362. static void ibs_init(void)
  363. {
  364. has_ibs = boot_cpu_has(X86_FEATURE_IBS);
  365. if (!has_ibs)
  366. return;
  367. if (init_ibs_nmi()) {
  368. has_ibs = 0;
  369. return;
  370. }
  371. printk(KERN_INFO "oprofile: AMD IBS detected\n");
  372. }
  373. static void ibs_exit(void)
  374. {
  375. if (!has_ibs)
  376. return;
  377. clear_ibs_nmi();
  378. }
  379. static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
  380. static int setup_ibs_files(struct super_block *sb, struct dentry *root)
  381. {
  382. struct dentry *dir;
  383. int ret = 0;
  384. /* architecture specific files */
  385. if (create_arch_files)
  386. ret = create_arch_files(sb, root);
  387. if (ret)
  388. return ret;
  389. if (!has_ibs)
  390. return ret;
  391. /* model specific files */
  392. /* setup some reasonable defaults */
  393. ibs_config.max_cnt_fetch = 250000;
  394. ibs_config.fetch_enabled = 0;
  395. ibs_config.max_cnt_op = 250000;
  396. ibs_config.op_enabled = 0;
  397. ibs_config.dispatched_ops = 1;
  398. dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
  399. oprofilefs_create_ulong(sb, dir, "enable",
  400. &ibs_config.fetch_enabled);
  401. oprofilefs_create_ulong(sb, dir, "max_count",
  402. &ibs_config.max_cnt_fetch);
  403. oprofilefs_create_ulong(sb, dir, "rand_enable",
  404. &ibs_config.rand_en);
  405. dir = oprofilefs_mkdir(sb, root, "ibs_op");
  406. oprofilefs_create_ulong(sb, dir, "enable",
  407. &ibs_config.op_enabled);
  408. oprofilefs_create_ulong(sb, dir, "max_count",
  409. &ibs_config.max_cnt_op);
  410. oprofilefs_create_ulong(sb, dir, "dispatched_ops",
  411. &ibs_config.dispatched_ops);
  412. return 0;
  413. }
  414. static int op_amd_init(struct oprofile_operations *ops)
  415. {
  416. ibs_init();
  417. create_arch_files = ops->create_files;
  418. ops->create_files = setup_ibs_files;
  419. return 0;
  420. }
  421. static void op_amd_exit(void)
  422. {
  423. ibs_exit();
  424. }
  425. #else
  426. /* no IBS support */
  427. static int op_amd_init(struct oprofile_operations *ops)
  428. {
  429. return 0;
  430. }
  431. static void op_amd_exit(void) {}
  432. #endif /* CONFIG_OPROFILE_IBS */
  433. struct op_x86_model_spec op_amd_spec = {
  434. .num_counters = NUM_COUNTERS,
  435. .num_controls = NUM_CONTROLS,
  436. .num_virt_counters = NUM_VIRT_COUNTERS,
  437. .num_virt_controls = NUM_VIRT_CONTROLS,
  438. .reserved = MSR_AMD_EVENTSEL_RESERVED,
  439. .event_mask = OP_EVENT_MASK,
  440. .init = op_amd_init,
  441. .exit = op_amd_exit,
  442. .fill_in_addresses = &op_amd_fill_in_addresses,
  443. .setup_ctrs = &op_amd_setup_ctrs,
  444. .check_ctrs = &op_amd_check_ctrs,
  445. .start = &op_amd_start,
  446. .stop = &op_amd_stop,
  447. .shutdown = &op_amd_shutdown,
  448. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  449. .switch_ctrl = &op_mux_switch_ctrl,
  450. #endif
  451. };