op_model_amd.c 14 KB

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