op_model_amd.c 14 KB

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