op_model_amd.c 13 KB

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