op_model_amd.c 13 KB

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