op_model_amd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 "op_x86_model.h"
  26. #include "op_counter.h"
  27. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  28. #define NUM_VIRT_COUNTERS 32
  29. #else
  30. #define NUM_VIRT_COUNTERS 0
  31. #endif
  32. #define OP_EVENT_MASK 0x0FFF
  33. #define OP_CTR_OVERFLOW (1ULL<<31)
  34. #define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
  35. static int num_counters;
  36. static unsigned long reset_value[OP_MAX_COUNTER];
  37. #define IBS_FETCH_SIZE 6
  38. #define IBS_OP_SIZE 12
  39. static u32 ibs_caps;
  40. struct ibs_config {
  41. unsigned long op_enabled;
  42. unsigned long fetch_enabled;
  43. unsigned long max_cnt_fetch;
  44. unsigned long max_cnt_op;
  45. unsigned long rand_en;
  46. unsigned long dispatched_ops;
  47. unsigned long branch_target;
  48. };
  49. struct ibs_state {
  50. u64 ibs_op_ctl;
  51. int branch_target;
  52. unsigned long sample_size;
  53. };
  54. static struct ibs_config ibs_config;
  55. static struct ibs_state ibs_state;
  56. /*
  57. * IBS randomization macros
  58. */
  59. #define IBS_RANDOM_BITS 12
  60. #define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1)
  61. #define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5))
  62. /*
  63. * 16-bit Linear Feedback Shift Register (LFSR)
  64. *
  65. * 16 14 13 11
  66. * Feedback polynomial = X + X + X + X + 1
  67. */
  68. static unsigned int lfsr_random(void)
  69. {
  70. static unsigned int lfsr_value = 0xF00D;
  71. unsigned int bit;
  72. /* Compute next bit to shift in */
  73. bit = ((lfsr_value >> 0) ^
  74. (lfsr_value >> 2) ^
  75. (lfsr_value >> 3) ^
  76. (lfsr_value >> 5)) & 0x0001;
  77. /* Advance to next register value */
  78. lfsr_value = (lfsr_value >> 1) | (bit << 15);
  79. return lfsr_value;
  80. }
  81. /*
  82. * IBS software randomization
  83. *
  84. * The IBS periodic op counter is randomized in software. The lower 12
  85. * bits of the 20 bit counter are randomized. IbsOpCurCnt is
  86. * initialized with a 12 bit random value.
  87. */
  88. static inline u64 op_amd_randomize_ibs_op(u64 val)
  89. {
  90. unsigned int random = lfsr_random();
  91. if (!(ibs_caps & IBS_CAPS_RDWROPCNT))
  92. /*
  93. * Work around if the hw can not write to IbsOpCurCnt
  94. *
  95. * Randomize the lower 8 bits of the 16 bit
  96. * IbsOpMaxCnt [15:0] value in the range of -128 to
  97. * +127 by adding/subtracting an offset to the
  98. * maximum count (IbsOpMaxCnt).
  99. *
  100. * To avoid over or underflows and protect upper bits
  101. * starting at bit 16, the initial value for
  102. * IbsOpMaxCnt must fit in the range from 0x0081 to
  103. * 0xff80.
  104. */
  105. val += (s8)(random >> 4);
  106. else
  107. val |= (u64)(random & IBS_RANDOM_MASK) << 32;
  108. return val;
  109. }
  110. static inline void
  111. op_amd_handle_ibs(struct pt_regs * const regs,
  112. struct op_msrs const * const msrs)
  113. {
  114. u64 val, ctl;
  115. struct op_entry entry;
  116. if (!ibs_caps)
  117. return;
  118. if (ibs_config.fetch_enabled) {
  119. rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
  120. if (ctl & IBS_FETCH_VAL) {
  121. rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
  122. oprofile_write_reserve(&entry, regs, val,
  123. IBS_FETCH_CODE, IBS_FETCH_SIZE);
  124. oprofile_add_data64(&entry, val);
  125. oprofile_add_data64(&entry, ctl);
  126. rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
  127. oprofile_add_data64(&entry, val);
  128. oprofile_write_commit(&entry);
  129. /* reenable the IRQ */
  130. ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT);
  131. ctl |= IBS_FETCH_ENABLE;
  132. wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
  133. }
  134. }
  135. if (ibs_config.op_enabled) {
  136. rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
  137. if (ctl & IBS_OP_VAL) {
  138. rdmsrl(MSR_AMD64_IBSOPRIP, val);
  139. oprofile_write_reserve(&entry, regs, val, IBS_OP_CODE,
  140. ibs_state.sample_size);
  141. oprofile_add_data64(&entry, val);
  142. rdmsrl(MSR_AMD64_IBSOPDATA, val);
  143. oprofile_add_data64(&entry, val);
  144. rdmsrl(MSR_AMD64_IBSOPDATA2, val);
  145. oprofile_add_data64(&entry, val);
  146. rdmsrl(MSR_AMD64_IBSOPDATA3, val);
  147. oprofile_add_data64(&entry, val);
  148. rdmsrl(MSR_AMD64_IBSDCLINAD, val);
  149. oprofile_add_data64(&entry, val);
  150. rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
  151. oprofile_add_data64(&entry, val);
  152. if (ibs_state.branch_target) {
  153. rdmsrl(MSR_AMD64_IBSBRTARGET, val);
  154. oprofile_add_data(&entry, (unsigned long)val);
  155. }
  156. oprofile_write_commit(&entry);
  157. /* reenable the IRQ */
  158. ctl = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
  159. wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
  160. }
  161. }
  162. }
  163. static inline void op_amd_start_ibs(void)
  164. {
  165. u64 val;
  166. if (!ibs_caps)
  167. return;
  168. memset(&ibs_state, 0, sizeof(ibs_state));
  169. /*
  170. * Note: Since the max count settings may out of range we
  171. * write back the actual used values so that userland can read
  172. * it.
  173. */
  174. if (ibs_config.fetch_enabled) {
  175. val = ibs_config.max_cnt_fetch >> 4;
  176. val = min(val, IBS_FETCH_MAX_CNT);
  177. ibs_config.max_cnt_fetch = val << 4;
  178. val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
  179. val |= IBS_FETCH_ENABLE;
  180. wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
  181. }
  182. if (ibs_config.op_enabled) {
  183. val = ibs_config.max_cnt_op >> 4;
  184. if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) {
  185. /*
  186. * IbsOpCurCnt not supported. See
  187. * op_amd_randomize_ibs_op() for details.
  188. */
  189. val = clamp(val, 0x0081ULL, 0xFF80ULL);
  190. ibs_config.max_cnt_op = val << 4;
  191. } else {
  192. /*
  193. * The start value is randomized with a
  194. * positive offset, we need to compensate it
  195. * with the half of the randomized range. Also
  196. * avoid underflows.
  197. */
  198. val += IBS_RANDOM_MAXCNT_OFFSET;
  199. if (ibs_caps & IBS_CAPS_OPCNTEXT)
  200. val = min(val, IBS_OP_MAX_CNT_EXT);
  201. else
  202. val = min(val, IBS_OP_MAX_CNT);
  203. ibs_config.max_cnt_op =
  204. (val - IBS_RANDOM_MAXCNT_OFFSET) << 4;
  205. }
  206. val = ((val & ~IBS_OP_MAX_CNT) << 4) | (val & IBS_OP_MAX_CNT);
  207. val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
  208. val |= IBS_OP_ENABLE;
  209. ibs_state.ibs_op_ctl = val;
  210. ibs_state.sample_size = IBS_OP_SIZE;
  211. if (ibs_config.branch_target) {
  212. ibs_state.branch_target = 1;
  213. ibs_state.sample_size++;
  214. }
  215. val = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
  216. wrmsrl(MSR_AMD64_IBSOPCTL, val);
  217. }
  218. }
  219. static void op_amd_stop_ibs(void)
  220. {
  221. if (!ibs_caps)
  222. return;
  223. if (ibs_config.fetch_enabled)
  224. /* clear max count and enable */
  225. wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
  226. if (ibs_config.op_enabled)
  227. /* clear max count and enable */
  228. wrmsrl(MSR_AMD64_IBSOPCTL, 0);
  229. }
  230. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  231. static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
  232. struct op_msrs const * const msrs)
  233. {
  234. u64 val;
  235. int i;
  236. /* enable active counters */
  237. for (i = 0; i < num_counters; ++i) {
  238. int virt = op_x86_phys_to_virt(i);
  239. if (!reset_value[virt])
  240. continue;
  241. rdmsrl(msrs->controls[i].addr, val);
  242. val &= model->reserved;
  243. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  244. wrmsrl(msrs->controls[i].addr, val);
  245. }
  246. }
  247. #endif
  248. /* functions for op_amd_spec */
  249. static void op_amd_shutdown(struct op_msrs const * const msrs)
  250. {
  251. int i;
  252. for (i = 0; i < num_counters; ++i) {
  253. if (!msrs->counters[i].addr)
  254. continue;
  255. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  256. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  257. }
  258. }
  259. static int op_amd_fill_in_addresses(struct op_msrs * const msrs)
  260. {
  261. int i;
  262. for (i = 0; i < num_counters; i++) {
  263. if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  264. goto fail;
  265. if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) {
  266. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  267. goto fail;
  268. }
  269. /* both registers must be reserved */
  270. if (num_counters == AMD64_NUM_COUNTERS_CORE) {
  271. msrs->counters[i].addr = MSR_F15H_PERF_CTR + (i << 1);
  272. msrs->controls[i].addr = MSR_F15H_PERF_CTL + (i << 1);
  273. } else {
  274. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  275. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  276. }
  277. continue;
  278. fail:
  279. if (!counter_config[i].enabled)
  280. continue;
  281. op_x86_warn_reserved(i);
  282. op_amd_shutdown(msrs);
  283. return -EBUSY;
  284. }
  285. return 0;
  286. }
  287. static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
  288. struct op_msrs const * const msrs)
  289. {
  290. u64 val;
  291. int i;
  292. /* setup reset_value */
  293. for (i = 0; i < OP_MAX_COUNTER; ++i) {
  294. if (counter_config[i].enabled
  295. && msrs->counters[op_x86_virt_to_phys(i)].addr)
  296. reset_value[i] = counter_config[i].count;
  297. else
  298. reset_value[i] = 0;
  299. }
  300. /* clear all counters */
  301. for (i = 0; i < num_counters; ++i) {
  302. if (!msrs->controls[i].addr)
  303. continue;
  304. rdmsrl(msrs->controls[i].addr, val);
  305. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  306. op_x86_warn_in_use(i);
  307. val &= model->reserved;
  308. wrmsrl(msrs->controls[i].addr, val);
  309. /*
  310. * avoid a false detection of ctr overflows in NMI
  311. * handler
  312. */
  313. wrmsrl(msrs->counters[i].addr, -1LL);
  314. }
  315. /* enable active counters */
  316. for (i = 0; i < num_counters; ++i) {
  317. int virt = op_x86_phys_to_virt(i);
  318. if (!reset_value[virt])
  319. continue;
  320. /* setup counter registers */
  321. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  322. /* setup control registers */
  323. rdmsrl(msrs->controls[i].addr, val);
  324. val &= model->reserved;
  325. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  326. wrmsrl(msrs->controls[i].addr, val);
  327. }
  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. /*
  380. * check and reserve APIC extended interrupt LVT offset for IBS if
  381. * available
  382. */
  383. static void init_ibs(void)
  384. {
  385. ibs_caps = get_ibs_caps();
  386. if (!ibs_caps)
  387. return;
  388. printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n", ibs_caps);
  389. }
  390. static int (*create_arch_files)(struct dentry *root);
  391. static int setup_ibs_files(struct dentry *root)
  392. {
  393. struct dentry *dir;
  394. int ret = 0;
  395. /* architecture specific files */
  396. if (create_arch_files)
  397. ret = create_arch_files(root);
  398. if (ret)
  399. return ret;
  400. if (!ibs_caps)
  401. return ret;
  402. /* model specific files */
  403. /* setup some reasonable defaults */
  404. memset(&ibs_config, 0, sizeof(ibs_config));
  405. ibs_config.max_cnt_fetch = 250000;
  406. ibs_config.max_cnt_op = 250000;
  407. if (ibs_caps & IBS_CAPS_FETCHSAM) {
  408. dir = oprofilefs_mkdir(root, "ibs_fetch");
  409. oprofilefs_create_ulong(dir, "enable",
  410. &ibs_config.fetch_enabled);
  411. oprofilefs_create_ulong(dir, "max_count",
  412. &ibs_config.max_cnt_fetch);
  413. oprofilefs_create_ulong(dir, "rand_enable",
  414. &ibs_config.rand_en);
  415. }
  416. if (ibs_caps & IBS_CAPS_OPSAM) {
  417. dir = oprofilefs_mkdir(root, "ibs_op");
  418. oprofilefs_create_ulong(dir, "enable",
  419. &ibs_config.op_enabled);
  420. oprofilefs_create_ulong(dir, "max_count",
  421. &ibs_config.max_cnt_op);
  422. if (ibs_caps & IBS_CAPS_OPCNT)
  423. oprofilefs_create_ulong(dir, "dispatched_ops",
  424. &ibs_config.dispatched_ops);
  425. if (ibs_caps & IBS_CAPS_BRNTRGT)
  426. oprofilefs_create_ulong(dir, "branch_target",
  427. &ibs_config.branch_target);
  428. }
  429. return 0;
  430. }
  431. struct op_x86_model_spec op_amd_spec;
  432. static int op_amd_init(struct oprofile_operations *ops)
  433. {
  434. init_ibs();
  435. create_arch_files = ops->create_files;
  436. ops->create_files = setup_ibs_files;
  437. if (boot_cpu_data.x86 == 0x15) {
  438. num_counters = AMD64_NUM_COUNTERS_CORE;
  439. } else {
  440. num_counters = AMD64_NUM_COUNTERS;
  441. }
  442. op_amd_spec.num_counters = num_counters;
  443. op_amd_spec.num_controls = num_counters;
  444. op_amd_spec.num_virt_counters = max(num_counters, NUM_VIRT_COUNTERS);
  445. return 0;
  446. }
  447. struct op_x86_model_spec op_amd_spec = {
  448. /* num_counters/num_controls filled in at runtime */
  449. .reserved = MSR_AMD_EVENTSEL_RESERVED,
  450. .event_mask = OP_EVENT_MASK,
  451. .init = op_amd_init,
  452. .fill_in_addresses = &op_amd_fill_in_addresses,
  453. .setup_ctrs = &op_amd_setup_ctrs,
  454. .check_ctrs = &op_amd_check_ctrs,
  455. .start = &op_amd_start,
  456. .stop = &op_amd_stop,
  457. .shutdown = &op_amd_shutdown,
  458. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  459. .switch_ctrl = &op_mux_switch_ctrl,
  460. #endif
  461. };