op_model_amd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. ibs_op_ctl |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
  222. ibs_op_ctl |= IBS_OP_ENABLE;
  223. val = op_amd_randomize_ibs_op(ibs_op_ctl);
  224. wrmsrl(MSR_AMD64_IBSOPCTL, val);
  225. }
  226. }
  227. static void op_amd_stop_ibs(void)
  228. {
  229. if (!ibs_caps)
  230. return;
  231. if (ibs_config.fetch_enabled)
  232. /* clear max count and enable */
  233. wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
  234. if (ibs_config.op_enabled)
  235. /* clear max count and enable */
  236. wrmsrl(MSR_AMD64_IBSOPCTL, 0);
  237. }
  238. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  239. static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
  240. struct op_msrs const * const msrs)
  241. {
  242. u64 val;
  243. int i;
  244. /* enable active counters */
  245. for (i = 0; i < NUM_COUNTERS; ++i) {
  246. int virt = op_x86_phys_to_virt(i);
  247. if (!reset_value[virt])
  248. continue;
  249. rdmsrl(msrs->controls[i].addr, val);
  250. val &= model->reserved;
  251. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  252. wrmsrl(msrs->controls[i].addr, val);
  253. }
  254. }
  255. #endif
  256. /* functions for op_amd_spec */
  257. static void op_amd_shutdown(struct op_msrs const * const msrs)
  258. {
  259. int i;
  260. for (i = 0; i < NUM_COUNTERS; ++i) {
  261. if (!msrs->counters[i].addr)
  262. continue;
  263. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  264. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  265. }
  266. }
  267. static int op_amd_fill_in_addresses(struct op_msrs * const msrs)
  268. {
  269. int i;
  270. for (i = 0; i < NUM_COUNTERS; i++) {
  271. if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  272. goto fail;
  273. if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) {
  274. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  275. goto fail;
  276. }
  277. /* both registers must be reserved */
  278. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  279. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  280. continue;
  281. fail:
  282. if (!counter_config[i].enabled)
  283. continue;
  284. op_x86_warn_reserved(i);
  285. op_amd_shutdown(msrs);
  286. return -EBUSY;
  287. }
  288. return 0;
  289. }
  290. static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
  291. struct op_msrs const * const msrs)
  292. {
  293. u64 val;
  294. int i;
  295. /* setup reset_value */
  296. for (i = 0; i < NUM_VIRT_COUNTERS; ++i) {
  297. if (counter_config[i].enabled
  298. && msrs->counters[op_x86_virt_to_phys(i)].addr)
  299. reset_value[i] = counter_config[i].count;
  300. else
  301. reset_value[i] = 0;
  302. }
  303. /* clear all counters */
  304. for (i = 0; i < NUM_COUNTERS; ++i) {
  305. if (!msrs->controls[i].addr)
  306. continue;
  307. rdmsrl(msrs->controls[i].addr, val);
  308. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  309. op_x86_warn_in_use(i);
  310. val &= model->reserved;
  311. wrmsrl(msrs->controls[i].addr, val);
  312. /*
  313. * avoid a false detection of ctr overflows in NMI
  314. * handler
  315. */
  316. wrmsrl(msrs->counters[i].addr, -1LL);
  317. }
  318. /* enable active counters */
  319. for (i = 0; i < NUM_COUNTERS; ++i) {
  320. int virt = op_x86_phys_to_virt(i);
  321. if (!reset_value[virt])
  322. continue;
  323. /* setup counter registers */
  324. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  325. /* setup control registers */
  326. rdmsrl(msrs->controls[i].addr, val);
  327. val &= model->reserved;
  328. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  329. wrmsrl(msrs->controls[i].addr, val);
  330. }
  331. if (ibs_caps)
  332. setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
  333. }
  334. static void op_amd_cpu_shutdown(void)
  335. {
  336. if (ibs_caps)
  337. setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
  338. }
  339. static int op_amd_check_ctrs(struct pt_regs * const regs,
  340. struct op_msrs const * const msrs)
  341. {
  342. u64 val;
  343. int i;
  344. for (i = 0; i < NUM_COUNTERS; ++i) {
  345. int virt = op_x86_phys_to_virt(i);
  346. if (!reset_value[virt])
  347. continue;
  348. rdmsrl(msrs->counters[i].addr, val);
  349. /* bit is clear if overflowed: */
  350. if (val & OP_CTR_OVERFLOW)
  351. continue;
  352. oprofile_add_sample(regs, virt);
  353. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  354. }
  355. op_amd_handle_ibs(regs, msrs);
  356. /* See op_model_ppro.c */
  357. return 1;
  358. }
  359. static void op_amd_start(struct op_msrs const * const msrs)
  360. {
  361. u64 val;
  362. int i;
  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_start_ibs();
  371. }
  372. static void op_amd_stop(struct op_msrs const * const msrs)
  373. {
  374. u64 val;
  375. int i;
  376. /*
  377. * Subtle: stop on all counters to avoid race with setting our
  378. * pm callback
  379. */
  380. for (i = 0; i < NUM_COUNTERS; ++i) {
  381. if (!reset_value[op_x86_phys_to_virt(i)])
  382. continue;
  383. rdmsrl(msrs->controls[i].addr, val);
  384. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  385. wrmsrl(msrs->controls[i].addr, val);
  386. }
  387. op_amd_stop_ibs();
  388. }
  389. static int __init_ibs_nmi(void)
  390. {
  391. #define IBSCTL_LVTOFFSETVAL (1 << 8)
  392. #define IBSCTL 0x1cc
  393. struct pci_dev *cpu_cfg;
  394. int nodes;
  395. u32 value = 0;
  396. u8 ibs_eilvt_off;
  397. ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
  398. nodes = 0;
  399. cpu_cfg = NULL;
  400. do {
  401. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  402. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  403. cpu_cfg);
  404. if (!cpu_cfg)
  405. break;
  406. ++nodes;
  407. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  408. | IBSCTL_LVTOFFSETVAL);
  409. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  410. if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
  411. pci_dev_put(cpu_cfg);
  412. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  413. "IBSCTL = 0x%08x", value);
  414. return 1;
  415. }
  416. } while (1);
  417. if (!nodes) {
  418. printk(KERN_DEBUG "No CPU node configured for IBS");
  419. return 1;
  420. }
  421. return 0;
  422. }
  423. /* initialize the APIC for the IBS interrupts if available */
  424. static void init_ibs(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 int (*create_arch_files)(struct super_block *sb, struct dentry *root);
  437. static int setup_ibs_files(struct super_block *sb, struct dentry *root)
  438. {
  439. struct dentry *dir;
  440. int ret = 0;
  441. /* architecture specific files */
  442. if (create_arch_files)
  443. ret = create_arch_files(sb, root);
  444. if (ret)
  445. return ret;
  446. if (!ibs_caps)
  447. return ret;
  448. /* model specific files */
  449. /* setup some reasonable defaults */
  450. ibs_config.max_cnt_fetch = 250000;
  451. ibs_config.fetch_enabled = 0;
  452. ibs_config.max_cnt_op = 250000;
  453. ibs_config.op_enabled = 0;
  454. ibs_config.dispatched_ops = 0;
  455. if (ibs_caps & IBS_CAPS_FETCHSAM) {
  456. dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
  457. oprofilefs_create_ulong(sb, dir, "enable",
  458. &ibs_config.fetch_enabled);
  459. oprofilefs_create_ulong(sb, dir, "max_count",
  460. &ibs_config.max_cnt_fetch);
  461. oprofilefs_create_ulong(sb, dir, "rand_enable",
  462. &ibs_config.rand_en);
  463. }
  464. if (ibs_caps & IBS_CAPS_OPSAM) {
  465. dir = oprofilefs_mkdir(sb, root, "ibs_op");
  466. oprofilefs_create_ulong(sb, dir, "enable",
  467. &ibs_config.op_enabled);
  468. oprofilefs_create_ulong(sb, dir, "max_count",
  469. &ibs_config.max_cnt_op);
  470. if (ibs_caps & IBS_CAPS_OPCNT)
  471. oprofilefs_create_ulong(sb, dir, "dispatched_ops",
  472. &ibs_config.dispatched_ops);
  473. }
  474. return 0;
  475. }
  476. static int op_amd_init(struct oprofile_operations *ops)
  477. {
  478. init_ibs();
  479. create_arch_files = ops->create_files;
  480. ops->create_files = setup_ibs_files;
  481. return 0;
  482. }
  483. struct op_x86_model_spec op_amd_spec = {
  484. .num_counters = NUM_COUNTERS,
  485. .num_controls = NUM_COUNTERS,
  486. .num_virt_counters = NUM_VIRT_COUNTERS,
  487. .reserved = MSR_AMD_EVENTSEL_RESERVED,
  488. .event_mask = OP_EVENT_MASK,
  489. .init = op_amd_init,
  490. .fill_in_addresses = &op_amd_fill_in_addresses,
  491. .setup_ctrs = &op_amd_setup_ctrs,
  492. .cpu_down = &op_amd_cpu_shutdown,
  493. .check_ctrs = &op_amd_check_ctrs,
  494. .start = &op_amd_start,
  495. .stop = &op_amd_stop,
  496. .shutdown = &op_amd_shutdown,
  497. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  498. .switch_ctrl = &op_mux_switch_ctrl,
  499. #endif
  500. };