op_model_amd.c 12 KB

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