op_model_amd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 <asm/processor.h>
  25. #include <asm/cpufeature.h>
  26. #include "op_x86_model.h"
  27. #include "op_counter.h"
  28. #define NUM_COUNTERS 4
  29. #define NUM_CONTROLS 4
  30. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  31. #define NUM_VIRT_COUNTERS 32
  32. #define NUM_VIRT_CONTROLS 32
  33. #else
  34. #define NUM_VIRT_COUNTERS NUM_COUNTERS
  35. #define NUM_VIRT_CONTROLS NUM_CONTROLS
  36. #endif
  37. #define OP_EVENT_MASK 0x0FFF
  38. #define OP_CTR_OVERFLOW (1ULL<<31)
  39. #define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
  40. static unsigned long reset_value[NUM_VIRT_COUNTERS];
  41. #define IBS_FETCH_SIZE 6
  42. #define IBS_OP_SIZE 12
  43. static u32 ibs_caps;
  44. struct op_ibs_config {
  45. unsigned long op_enabled;
  46. unsigned long fetch_enabled;
  47. unsigned long max_cnt_fetch;
  48. unsigned long max_cnt_op;
  49. unsigned long rand_en;
  50. unsigned long dispatched_ops;
  51. };
  52. static struct op_ibs_config ibs_config;
  53. static u64 ibs_op_ctl;
  54. /*
  55. * IBS cpuid feature detection
  56. */
  57. #define IBS_CPUID_FEATURES 0x8000001b
  58. /*
  59. * Same bit mask as for IBS cpuid feature flags (Fn8000_001B_EAX), but
  60. * bit 0 is used to indicate the existence of IBS.
  61. */
  62. #define IBS_CAPS_AVAIL (1LL<<0)
  63. #define IBS_CAPS_RDWROPCNT (1LL<<3)
  64. #define IBS_CAPS_OPCNT (1LL<<4)
  65. /*
  66. * IBS randomization macros
  67. */
  68. #define IBS_RANDOM_BITS 12
  69. #define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1)
  70. #define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5))
  71. static u32 get_ibs_caps(void)
  72. {
  73. u32 ibs_caps;
  74. unsigned int max_level;
  75. if (!boot_cpu_has(X86_FEATURE_IBS))
  76. return 0;
  77. /* check IBS cpuid feature flags */
  78. max_level = cpuid_eax(0x80000000);
  79. if (max_level < IBS_CPUID_FEATURES)
  80. return IBS_CAPS_AVAIL;
  81. ibs_caps = cpuid_eax(IBS_CPUID_FEATURES);
  82. if (!(ibs_caps & IBS_CAPS_AVAIL))
  83. /* cpuid flags not valid */
  84. return IBS_CAPS_AVAIL;
  85. return ibs_caps;
  86. }
  87. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  88. static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
  89. struct op_msrs const * const msrs)
  90. {
  91. u64 val;
  92. int i;
  93. /* enable active counters */
  94. for (i = 0; i < NUM_COUNTERS; ++i) {
  95. int virt = op_x86_phys_to_virt(i);
  96. if (!reset_value[virt])
  97. continue;
  98. rdmsrl(msrs->controls[i].addr, val);
  99. val &= model->reserved;
  100. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  101. wrmsrl(msrs->controls[i].addr, val);
  102. }
  103. }
  104. #endif
  105. /* functions for op_amd_spec */
  106. static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
  107. {
  108. int i;
  109. for (i = 0; i < NUM_COUNTERS; i++) {
  110. if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  111. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  112. }
  113. for (i = 0; i < NUM_CONTROLS; i++) {
  114. if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
  115. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  116. }
  117. }
  118. static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
  119. struct op_msrs const * const msrs)
  120. {
  121. u64 val;
  122. int i;
  123. /* setup reset_value */
  124. for (i = 0; i < NUM_VIRT_COUNTERS; ++i) {
  125. if (counter_config[i].enabled
  126. && msrs->counters[op_x86_virt_to_phys(i)].addr)
  127. reset_value[i] = counter_config[i].count;
  128. else
  129. reset_value[i] = 0;
  130. }
  131. /* clear all counters */
  132. for (i = 0; i < NUM_CONTROLS; ++i) {
  133. if (unlikely(!msrs->controls[i].addr)) {
  134. if (counter_config[i].enabled && !smp_processor_id())
  135. /*
  136. * counter is reserved, this is on all
  137. * cpus, so report only for cpu #0
  138. */
  139. op_x86_warn_reserved(i);
  140. continue;
  141. }
  142. rdmsrl(msrs->controls[i].addr, val);
  143. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  144. op_x86_warn_in_use(i);
  145. val &= model->reserved;
  146. wrmsrl(msrs->controls[i].addr, val);
  147. }
  148. /* avoid a false detection of ctr overflows in NMI handler */
  149. for (i = 0; i < NUM_COUNTERS; ++i) {
  150. if (unlikely(!msrs->counters[i].addr))
  151. continue;
  152. wrmsrl(msrs->counters[i].addr, -1LL);
  153. }
  154. /* enable active counters */
  155. for (i = 0; i < NUM_COUNTERS; ++i) {
  156. int virt = op_x86_phys_to_virt(i);
  157. if (!reset_value[virt])
  158. continue;
  159. /* setup counter registers */
  160. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  161. /* setup control registers */
  162. rdmsrl(msrs->controls[i].addr, val);
  163. val &= model->reserved;
  164. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  165. wrmsrl(msrs->controls[i].addr, val);
  166. }
  167. }
  168. /*
  169. * 16-bit Linear Feedback Shift Register (LFSR)
  170. *
  171. * 16 14 13 11
  172. * Feedback polynomial = X + X + X + X + 1
  173. */
  174. static unsigned int lfsr_random(void)
  175. {
  176. static unsigned int lfsr_value = 0xF00D;
  177. unsigned int bit;
  178. /* Compute next bit to shift in */
  179. bit = ((lfsr_value >> 0) ^
  180. (lfsr_value >> 2) ^
  181. (lfsr_value >> 3) ^
  182. (lfsr_value >> 5)) & 0x0001;
  183. /* Advance to next register value */
  184. lfsr_value = (lfsr_value >> 1) | (bit << 15);
  185. return lfsr_value;
  186. }
  187. /*
  188. * IBS software randomization
  189. *
  190. * The IBS periodic op counter is randomized in software. The lower 12
  191. * bits of the 20 bit counter are randomized. IbsOpCurCnt is
  192. * initialized with a 12 bit random value.
  193. */
  194. static inline u64 op_amd_randomize_ibs_op(u64 val)
  195. {
  196. unsigned int random = lfsr_random();
  197. if (!(ibs_caps & IBS_CAPS_RDWROPCNT))
  198. /*
  199. * Work around if the hw can not write to IbsOpCurCnt
  200. *
  201. * Randomize the lower 8 bits of the 16 bit
  202. * IbsOpMaxCnt [15:0] value in the range of -128 to
  203. * +127 by adding/subtracting an offset to the
  204. * maximum count (IbsOpMaxCnt).
  205. *
  206. * To avoid over or underflows and protect upper bits
  207. * starting at bit 16, the initial value for
  208. * IbsOpMaxCnt must fit in the range from 0x0081 to
  209. * 0xff80.
  210. */
  211. val += (s8)(random >> 4);
  212. else
  213. val |= (u64)(random & IBS_RANDOM_MASK) << 32;
  214. return val;
  215. }
  216. static inline void
  217. op_amd_handle_ibs(struct pt_regs * const regs,
  218. struct op_msrs const * const msrs)
  219. {
  220. u64 val, ctl;
  221. struct op_entry entry;
  222. if (!ibs_caps)
  223. return;
  224. if (ibs_config.fetch_enabled) {
  225. rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
  226. if (ctl & IBS_FETCH_VAL) {
  227. rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
  228. oprofile_write_reserve(&entry, regs, val,
  229. IBS_FETCH_CODE, IBS_FETCH_SIZE);
  230. oprofile_add_data64(&entry, val);
  231. oprofile_add_data64(&entry, ctl);
  232. rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
  233. oprofile_add_data64(&entry, val);
  234. oprofile_write_commit(&entry);
  235. /* reenable the IRQ */
  236. ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT);
  237. ctl |= IBS_FETCH_ENABLE;
  238. wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
  239. }
  240. }
  241. if (ibs_config.op_enabled) {
  242. rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
  243. if (ctl & IBS_OP_VAL) {
  244. rdmsrl(MSR_AMD64_IBSOPRIP, val);
  245. oprofile_write_reserve(&entry, regs, val,
  246. IBS_OP_CODE, IBS_OP_SIZE);
  247. oprofile_add_data64(&entry, val);
  248. rdmsrl(MSR_AMD64_IBSOPDATA, val);
  249. oprofile_add_data64(&entry, val);
  250. rdmsrl(MSR_AMD64_IBSOPDATA2, val);
  251. oprofile_add_data64(&entry, val);
  252. rdmsrl(MSR_AMD64_IBSOPDATA3, val);
  253. oprofile_add_data64(&entry, val);
  254. rdmsrl(MSR_AMD64_IBSDCLINAD, val);
  255. oprofile_add_data64(&entry, val);
  256. rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
  257. oprofile_add_data64(&entry, val);
  258. oprofile_write_commit(&entry);
  259. /* reenable the IRQ */
  260. ctl = op_amd_randomize_ibs_op(ibs_op_ctl);
  261. wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
  262. }
  263. }
  264. }
  265. static inline void op_amd_start_ibs(void)
  266. {
  267. u64 val;
  268. if (!ibs_caps)
  269. return;
  270. if (ibs_config.fetch_enabled) {
  271. val = (ibs_config.max_cnt_fetch >> 4) & IBS_FETCH_MAX_CNT;
  272. val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
  273. val |= IBS_FETCH_ENABLE;
  274. wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
  275. }
  276. if (ibs_config.op_enabled) {
  277. ibs_op_ctl = ibs_config.max_cnt_op >> 4;
  278. if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) {
  279. /*
  280. * IbsOpCurCnt not supported. See
  281. * op_amd_randomize_ibs_op() for details.
  282. */
  283. ibs_op_ctl = clamp(ibs_op_ctl, 0x0081ULL, 0xFF80ULL);
  284. } else {
  285. /*
  286. * The start value is randomized with a
  287. * positive offset, we need to compensate it
  288. * with the half of the randomized range. Also
  289. * avoid underflows.
  290. */
  291. ibs_op_ctl = min(ibs_op_ctl + IBS_RANDOM_MAXCNT_OFFSET,
  292. IBS_OP_MAX_CNT);
  293. }
  294. if (ibs_caps & IBS_CAPS_OPCNT && ibs_config.dispatched_ops)
  295. ibs_op_ctl |= IBS_OP_CNT_CTL;
  296. ibs_op_ctl |= IBS_OP_ENABLE;
  297. val = op_amd_randomize_ibs_op(ibs_op_ctl);
  298. wrmsrl(MSR_AMD64_IBSOPCTL, val);
  299. }
  300. }
  301. static void op_amd_stop_ibs(void)
  302. {
  303. if (!ibs_caps)
  304. return;
  305. if (ibs_config.fetch_enabled)
  306. /* clear max count and enable */
  307. wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
  308. if (ibs_config.op_enabled)
  309. /* clear max count and enable */
  310. wrmsrl(MSR_AMD64_IBSOPCTL, 0);
  311. }
  312. static int op_amd_check_ctrs(struct pt_regs * const regs,
  313. struct op_msrs const * const msrs)
  314. {
  315. u64 val;
  316. int i;
  317. for (i = 0; i < NUM_COUNTERS; ++i) {
  318. int virt = op_x86_phys_to_virt(i);
  319. if (!reset_value[virt])
  320. continue;
  321. rdmsrl(msrs->counters[i].addr, val);
  322. /* bit is clear if overflowed: */
  323. if (val & OP_CTR_OVERFLOW)
  324. continue;
  325. oprofile_add_sample(regs, virt);
  326. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  327. }
  328. op_amd_handle_ibs(regs, msrs);
  329. /* See op_model_ppro.c */
  330. return 1;
  331. }
  332. static void op_amd_start(struct op_msrs const * const msrs)
  333. {
  334. u64 val;
  335. int i;
  336. for (i = 0; i < NUM_COUNTERS; ++i) {
  337. if (!reset_value[op_x86_phys_to_virt(i)])
  338. continue;
  339. rdmsrl(msrs->controls[i].addr, val);
  340. val |= ARCH_PERFMON_EVENTSEL_ENABLE;
  341. wrmsrl(msrs->controls[i].addr, val);
  342. }
  343. op_amd_start_ibs();
  344. }
  345. static void op_amd_stop(struct op_msrs const * const msrs)
  346. {
  347. u64 val;
  348. int i;
  349. /*
  350. * Subtle: stop on all counters to avoid race with setting our
  351. * pm callback
  352. */
  353. for (i = 0; i < NUM_COUNTERS; ++i) {
  354. if (!reset_value[op_x86_phys_to_virt(i)])
  355. continue;
  356. rdmsrl(msrs->controls[i].addr, val);
  357. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  358. wrmsrl(msrs->controls[i].addr, val);
  359. }
  360. op_amd_stop_ibs();
  361. }
  362. static void op_amd_shutdown(struct op_msrs const * const msrs)
  363. {
  364. int i;
  365. for (i = 0; i < NUM_COUNTERS; ++i) {
  366. if (msrs->counters[i].addr)
  367. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  368. }
  369. for (i = 0; i < NUM_CONTROLS; ++i) {
  370. if (msrs->controls[i].addr)
  371. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  372. }
  373. }
  374. static u8 ibs_eilvt_off;
  375. static inline void apic_init_ibs_nmi_per_cpu(void *arg)
  376. {
  377. ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
  378. }
  379. static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
  380. {
  381. setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
  382. }
  383. static int init_ibs_nmi(void)
  384. {
  385. #define IBSCTL_LVTOFFSETVAL (1 << 8)
  386. #define IBSCTL 0x1cc
  387. struct pci_dev *cpu_cfg;
  388. int nodes;
  389. u32 value = 0;
  390. /* per CPU setup */
  391. on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
  392. nodes = 0;
  393. cpu_cfg = NULL;
  394. do {
  395. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  396. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  397. cpu_cfg);
  398. if (!cpu_cfg)
  399. break;
  400. ++nodes;
  401. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  402. | IBSCTL_LVTOFFSETVAL);
  403. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  404. if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
  405. pci_dev_put(cpu_cfg);
  406. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  407. "IBSCTL = 0x%08x", value);
  408. return 1;
  409. }
  410. } while (1);
  411. if (!nodes) {
  412. printk(KERN_DEBUG "No CPU node configured for IBS");
  413. return 1;
  414. }
  415. return 0;
  416. }
  417. /* uninitialize the APIC for the IBS interrupts if needed */
  418. static void clear_ibs_nmi(void)
  419. {
  420. if (ibs_caps)
  421. on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
  422. }
  423. /* initialize the APIC for the IBS interrupts if available */
  424. static void ibs_init(void)
  425. {
  426. ibs_caps = get_ibs_caps();
  427. if (!ibs_caps)
  428. return;
  429. if (init_ibs_nmi()) {
  430. ibs_caps = 0;
  431. return;
  432. }
  433. printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n",
  434. (unsigned)ibs_caps);
  435. }
  436. static void ibs_exit(void)
  437. {
  438. if (!ibs_caps)
  439. return;
  440. clear_ibs_nmi();
  441. }
  442. static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
  443. static int setup_ibs_files(struct super_block *sb, struct dentry *root)
  444. {
  445. struct dentry *dir;
  446. int ret = 0;
  447. /* architecture specific files */
  448. if (create_arch_files)
  449. ret = create_arch_files(sb, root);
  450. if (ret)
  451. return ret;
  452. if (!ibs_caps)
  453. return ret;
  454. /* model specific files */
  455. /* setup some reasonable defaults */
  456. ibs_config.max_cnt_fetch = 250000;
  457. ibs_config.fetch_enabled = 0;
  458. ibs_config.max_cnt_op = 250000;
  459. ibs_config.op_enabled = 0;
  460. ibs_config.dispatched_ops = 0;
  461. dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
  462. oprofilefs_create_ulong(sb, dir, "enable",
  463. &ibs_config.fetch_enabled);
  464. oprofilefs_create_ulong(sb, dir, "max_count",
  465. &ibs_config.max_cnt_fetch);
  466. oprofilefs_create_ulong(sb, dir, "rand_enable",
  467. &ibs_config.rand_en);
  468. dir = oprofilefs_mkdir(sb, root, "ibs_op");
  469. oprofilefs_create_ulong(sb, dir, "enable",
  470. &ibs_config.op_enabled);
  471. oprofilefs_create_ulong(sb, dir, "max_count",
  472. &ibs_config.max_cnt_op);
  473. if (ibs_caps & IBS_CAPS_OPCNT)
  474. oprofilefs_create_ulong(sb, dir, "dispatched_ops",
  475. &ibs_config.dispatched_ops);
  476. return 0;
  477. }
  478. static int op_amd_init(struct oprofile_operations *ops)
  479. {
  480. ibs_init();
  481. create_arch_files = ops->create_files;
  482. ops->create_files = setup_ibs_files;
  483. return 0;
  484. }
  485. static void op_amd_exit(void)
  486. {
  487. ibs_exit();
  488. }
  489. struct op_x86_model_spec op_amd_spec = {
  490. .num_counters = NUM_COUNTERS,
  491. .num_controls = NUM_CONTROLS,
  492. .num_virt_counters = NUM_VIRT_COUNTERS,
  493. .reserved = MSR_AMD_EVENTSEL_RESERVED,
  494. .event_mask = OP_EVENT_MASK,
  495. .init = op_amd_init,
  496. .exit = op_amd_exit,
  497. .fill_in_addresses = &op_amd_fill_in_addresses,
  498. .setup_ctrs = &op_amd_setup_ctrs,
  499. .check_ctrs = &op_amd_check_ctrs,
  500. .start = &op_amd_start,
  501. .stop = &op_amd_stop,
  502. .shutdown = &op_amd_shutdown,
  503. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  504. .switch_ctrl = &op_mux_switch_ctrl,
  505. #endif
  506. };