op_model_amd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  29. #define NUM_VIRT_COUNTERS 32
  30. #else
  31. #define NUM_VIRT_COUNTERS 0
  32. #endif
  33. #define OP_EVENT_MASK 0x0FFF
  34. #define OP_CTR_OVERFLOW (1ULL<<31)
  35. #define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
  36. static int num_counters;
  37. static unsigned long reset_value[OP_MAX_COUNTER];
  38. #define IBS_FETCH_SIZE 6
  39. #define IBS_OP_SIZE 12
  40. static u32 ibs_caps;
  41. struct 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. unsigned long branch_target;
  49. };
  50. struct ibs_state {
  51. u64 ibs_op_ctl;
  52. int branch_target;
  53. unsigned long sample_size;
  54. };
  55. static struct ibs_config ibs_config;
  56. static struct ibs_state ibs_state;
  57. /*
  58. * IBS randomization macros
  59. */
  60. #define IBS_RANDOM_BITS 12
  61. #define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1)
  62. #define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5))
  63. static u32 get_ibs_caps(void)
  64. {
  65. u32 ibs_caps;
  66. unsigned int max_level;
  67. if (!boot_cpu_has(X86_FEATURE_IBS))
  68. return 0;
  69. /* check IBS cpuid feature flags */
  70. max_level = cpuid_eax(0x80000000);
  71. if (max_level < IBS_CPUID_FEATURES)
  72. return IBS_CAPS_DEFAULT;
  73. ibs_caps = cpuid_eax(IBS_CPUID_FEATURES);
  74. if (!(ibs_caps & IBS_CAPS_AVAIL))
  75. /* cpuid flags not valid */
  76. return IBS_CAPS_DEFAULT;
  77. return ibs_caps;
  78. }
  79. /*
  80. * 16-bit Linear Feedback Shift Register (LFSR)
  81. *
  82. * 16 14 13 11
  83. * Feedback polynomial = X + X + X + X + 1
  84. */
  85. static unsigned int lfsr_random(void)
  86. {
  87. static unsigned int lfsr_value = 0xF00D;
  88. unsigned int bit;
  89. /* Compute next bit to shift in */
  90. bit = ((lfsr_value >> 0) ^
  91. (lfsr_value >> 2) ^
  92. (lfsr_value >> 3) ^
  93. (lfsr_value >> 5)) & 0x0001;
  94. /* Advance to next register value */
  95. lfsr_value = (lfsr_value >> 1) | (bit << 15);
  96. return lfsr_value;
  97. }
  98. /*
  99. * IBS software randomization
  100. *
  101. * The IBS periodic op counter is randomized in software. The lower 12
  102. * bits of the 20 bit counter are randomized. IbsOpCurCnt is
  103. * initialized with a 12 bit random value.
  104. */
  105. static inline u64 op_amd_randomize_ibs_op(u64 val)
  106. {
  107. unsigned int random = lfsr_random();
  108. if (!(ibs_caps & IBS_CAPS_RDWROPCNT))
  109. /*
  110. * Work around if the hw can not write to IbsOpCurCnt
  111. *
  112. * Randomize the lower 8 bits of the 16 bit
  113. * IbsOpMaxCnt [15:0] value in the range of -128 to
  114. * +127 by adding/subtracting an offset to the
  115. * maximum count (IbsOpMaxCnt).
  116. *
  117. * To avoid over or underflows and protect upper bits
  118. * starting at bit 16, the initial value for
  119. * IbsOpMaxCnt must fit in the range from 0x0081 to
  120. * 0xff80.
  121. */
  122. val += (s8)(random >> 4);
  123. else
  124. val |= (u64)(random & IBS_RANDOM_MASK) << 32;
  125. return val;
  126. }
  127. static inline void
  128. op_amd_handle_ibs(struct pt_regs * const regs,
  129. struct op_msrs const * const msrs)
  130. {
  131. u64 val, ctl;
  132. struct op_entry entry;
  133. if (!ibs_caps)
  134. return;
  135. if (ibs_config.fetch_enabled) {
  136. rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
  137. if (ctl & IBS_FETCH_VAL) {
  138. rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
  139. oprofile_write_reserve(&entry, regs, val,
  140. IBS_FETCH_CODE, IBS_FETCH_SIZE);
  141. oprofile_add_data64(&entry, val);
  142. oprofile_add_data64(&entry, ctl);
  143. rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
  144. oprofile_add_data64(&entry, val);
  145. oprofile_write_commit(&entry);
  146. /* reenable the IRQ */
  147. ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT);
  148. ctl |= IBS_FETCH_ENABLE;
  149. wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
  150. }
  151. }
  152. if (ibs_config.op_enabled) {
  153. rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
  154. if (ctl & IBS_OP_VAL) {
  155. rdmsrl(MSR_AMD64_IBSOPRIP, val);
  156. oprofile_write_reserve(&entry, regs, val, IBS_OP_CODE,
  157. ibs_state.sample_size);
  158. oprofile_add_data64(&entry, val);
  159. rdmsrl(MSR_AMD64_IBSOPDATA, val);
  160. oprofile_add_data64(&entry, val);
  161. rdmsrl(MSR_AMD64_IBSOPDATA2, val);
  162. oprofile_add_data64(&entry, val);
  163. rdmsrl(MSR_AMD64_IBSOPDATA3, val);
  164. oprofile_add_data64(&entry, val);
  165. rdmsrl(MSR_AMD64_IBSDCLINAD, val);
  166. oprofile_add_data64(&entry, val);
  167. rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
  168. oprofile_add_data64(&entry, val);
  169. if (ibs_state.branch_target) {
  170. rdmsrl(MSR_AMD64_IBSBRTARGET, val);
  171. oprofile_add_data(&entry, (unsigned long)val);
  172. }
  173. oprofile_write_commit(&entry);
  174. /* reenable the IRQ */
  175. ctl = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
  176. wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
  177. }
  178. }
  179. }
  180. static inline void op_amd_start_ibs(void)
  181. {
  182. u64 val;
  183. if (!ibs_caps)
  184. return;
  185. memset(&ibs_state, 0, sizeof(ibs_state));
  186. /*
  187. * Note: Since the max count settings may out of range we
  188. * write back the actual used values so that userland can read
  189. * it.
  190. */
  191. if (ibs_config.fetch_enabled) {
  192. val = ibs_config.max_cnt_fetch >> 4;
  193. val = min(val, IBS_FETCH_MAX_CNT);
  194. ibs_config.max_cnt_fetch = val << 4;
  195. val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
  196. val |= IBS_FETCH_ENABLE;
  197. wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
  198. }
  199. if (ibs_config.op_enabled) {
  200. val = ibs_config.max_cnt_op >> 4;
  201. if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) {
  202. /*
  203. * IbsOpCurCnt not supported. See
  204. * op_amd_randomize_ibs_op() for details.
  205. */
  206. val = clamp(val, 0x0081ULL, 0xFF80ULL);
  207. ibs_config.max_cnt_op = val << 4;
  208. } else {
  209. /*
  210. * The start value is randomized with a
  211. * positive offset, we need to compensate it
  212. * with the half of the randomized range. Also
  213. * avoid underflows.
  214. */
  215. val += IBS_RANDOM_MAXCNT_OFFSET;
  216. if (ibs_caps & IBS_CAPS_OPCNTEXT)
  217. val = min(val, IBS_OP_MAX_CNT_EXT);
  218. else
  219. val = min(val, IBS_OP_MAX_CNT);
  220. ibs_config.max_cnt_op =
  221. (val - IBS_RANDOM_MAXCNT_OFFSET) << 4;
  222. }
  223. val = ((val & ~IBS_OP_MAX_CNT) << 4) | (val & IBS_OP_MAX_CNT);
  224. val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
  225. val |= IBS_OP_ENABLE;
  226. ibs_state.ibs_op_ctl = val;
  227. ibs_state.sample_size = IBS_OP_SIZE;
  228. if (ibs_config.branch_target) {
  229. ibs_state.branch_target = 1;
  230. ibs_state.sample_size++;
  231. }
  232. val = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
  233. wrmsrl(MSR_AMD64_IBSOPCTL, val);
  234. }
  235. }
  236. static void op_amd_stop_ibs(void)
  237. {
  238. if (!ibs_caps)
  239. return;
  240. if (ibs_config.fetch_enabled)
  241. /* clear max count and enable */
  242. wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
  243. if (ibs_config.op_enabled)
  244. /* clear max count and enable */
  245. wrmsrl(MSR_AMD64_IBSOPCTL, 0);
  246. }
  247. static inline int get_eilvt(int offset)
  248. {
  249. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  250. }
  251. static inline int put_eilvt(int offset)
  252. {
  253. return !setup_APIC_eilvt(offset, 0, 0, 1);
  254. }
  255. static inline int ibs_eilvt_valid(void)
  256. {
  257. int offset;
  258. u64 val;
  259. int valid = 0;
  260. preempt_disable();
  261. rdmsrl(MSR_AMD64_IBSCTL, val);
  262. offset = val & IBSCTL_LVT_OFFSET_MASK;
  263. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  264. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  265. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  266. goto out;
  267. }
  268. if (!get_eilvt(offset)) {
  269. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  270. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  271. goto out;
  272. }
  273. valid = 1;
  274. out:
  275. preempt_enable();
  276. return valid;
  277. }
  278. static inline int get_ibs_offset(void)
  279. {
  280. u64 val;
  281. rdmsrl(MSR_AMD64_IBSCTL, val);
  282. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  283. return -EINVAL;
  284. return val & IBSCTL_LVT_OFFSET_MASK;
  285. }
  286. static void setup_APIC_ibs(void)
  287. {
  288. int offset;
  289. offset = get_ibs_offset();
  290. if (offset < 0)
  291. goto failed;
  292. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  293. return;
  294. failed:
  295. pr_warn("oprofile: IBS APIC setup failed on cpu #%d\n",
  296. smp_processor_id());
  297. }
  298. static void clear_APIC_ibs(void)
  299. {
  300. int offset;
  301. offset = get_ibs_offset();
  302. if (offset >= 0)
  303. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  304. }
  305. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  306. static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
  307. struct op_msrs const * const msrs)
  308. {
  309. u64 val;
  310. int i;
  311. /* enable active counters */
  312. for (i = 0; i < num_counters; ++i) {
  313. int virt = op_x86_phys_to_virt(i);
  314. if (!reset_value[virt])
  315. continue;
  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. }
  322. #endif
  323. /* functions for op_amd_spec */
  324. static void op_amd_shutdown(struct op_msrs const * const msrs)
  325. {
  326. int i;
  327. for (i = 0; i < num_counters; ++i) {
  328. if (!msrs->counters[i].addr)
  329. continue;
  330. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  331. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  332. }
  333. }
  334. static int op_amd_fill_in_addresses(struct op_msrs * const msrs)
  335. {
  336. int i;
  337. for (i = 0; i < num_counters; i++) {
  338. if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  339. goto fail;
  340. if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) {
  341. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  342. goto fail;
  343. }
  344. /* both registers must be reserved */
  345. if (num_counters == AMD64_NUM_COUNTERS_F15H) {
  346. msrs->counters[i].addr = MSR_F15H_PERF_CTR + (i << 1);
  347. msrs->controls[i].addr = MSR_F15H_PERF_CTL + (i << 1);
  348. } else {
  349. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  350. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  351. }
  352. continue;
  353. fail:
  354. if (!counter_config[i].enabled)
  355. continue;
  356. op_x86_warn_reserved(i);
  357. op_amd_shutdown(msrs);
  358. return -EBUSY;
  359. }
  360. return 0;
  361. }
  362. static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
  363. struct op_msrs const * const msrs)
  364. {
  365. u64 val;
  366. int i;
  367. /* setup reset_value */
  368. for (i = 0; i < OP_MAX_COUNTER; ++i) {
  369. if (counter_config[i].enabled
  370. && msrs->counters[op_x86_virt_to_phys(i)].addr)
  371. reset_value[i] = counter_config[i].count;
  372. else
  373. reset_value[i] = 0;
  374. }
  375. /* clear all counters */
  376. for (i = 0; i < num_counters; ++i) {
  377. if (!msrs->controls[i].addr)
  378. continue;
  379. rdmsrl(msrs->controls[i].addr, val);
  380. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  381. op_x86_warn_in_use(i);
  382. val &= model->reserved;
  383. wrmsrl(msrs->controls[i].addr, val);
  384. /*
  385. * avoid a false detection of ctr overflows in NMI
  386. * handler
  387. */
  388. wrmsrl(msrs->counters[i].addr, -1LL);
  389. }
  390. /* enable active counters */
  391. for (i = 0; i < num_counters; ++i) {
  392. int virt = op_x86_phys_to_virt(i);
  393. if (!reset_value[virt])
  394. continue;
  395. /* setup counter registers */
  396. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  397. /* setup control registers */
  398. rdmsrl(msrs->controls[i].addr, val);
  399. val &= model->reserved;
  400. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  401. wrmsrl(msrs->controls[i].addr, val);
  402. }
  403. if (ibs_caps)
  404. setup_APIC_ibs();
  405. }
  406. static void op_amd_cpu_shutdown(void)
  407. {
  408. if (ibs_caps)
  409. clear_APIC_ibs();
  410. }
  411. static int op_amd_check_ctrs(struct pt_regs * const regs,
  412. struct op_msrs const * const msrs)
  413. {
  414. u64 val;
  415. int i;
  416. for (i = 0; i < num_counters; ++i) {
  417. int virt = op_x86_phys_to_virt(i);
  418. if (!reset_value[virt])
  419. continue;
  420. rdmsrl(msrs->counters[i].addr, val);
  421. /* bit is clear if overflowed: */
  422. if (val & OP_CTR_OVERFLOW)
  423. continue;
  424. oprofile_add_sample(regs, virt);
  425. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  426. }
  427. op_amd_handle_ibs(regs, msrs);
  428. /* See op_model_ppro.c */
  429. return 1;
  430. }
  431. static void op_amd_start(struct op_msrs const * const msrs)
  432. {
  433. u64 val;
  434. int i;
  435. for (i = 0; i < num_counters; ++i) {
  436. if (!reset_value[op_x86_phys_to_virt(i)])
  437. continue;
  438. rdmsrl(msrs->controls[i].addr, val);
  439. val |= ARCH_PERFMON_EVENTSEL_ENABLE;
  440. wrmsrl(msrs->controls[i].addr, val);
  441. }
  442. op_amd_start_ibs();
  443. }
  444. static void op_amd_stop(struct op_msrs const * const msrs)
  445. {
  446. u64 val;
  447. int i;
  448. /*
  449. * Subtle: stop on all counters to avoid race with setting our
  450. * pm callback
  451. */
  452. for (i = 0; i < num_counters; ++i) {
  453. if (!reset_value[op_x86_phys_to_virt(i)])
  454. continue;
  455. rdmsrl(msrs->controls[i].addr, val);
  456. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  457. wrmsrl(msrs->controls[i].addr, val);
  458. }
  459. op_amd_stop_ibs();
  460. }
  461. static int setup_ibs_ctl(int ibs_eilvt_off)
  462. {
  463. struct pci_dev *cpu_cfg;
  464. int nodes;
  465. u32 value = 0;
  466. nodes = 0;
  467. cpu_cfg = NULL;
  468. do {
  469. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  470. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  471. cpu_cfg);
  472. if (!cpu_cfg)
  473. break;
  474. ++nodes;
  475. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  476. | IBSCTL_LVT_OFFSET_VALID);
  477. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  478. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  479. pci_dev_put(cpu_cfg);
  480. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  481. "IBSCTL = 0x%08x\n", value);
  482. return -EINVAL;
  483. }
  484. } while (1);
  485. if (!nodes) {
  486. printk(KERN_DEBUG "No CPU node configured for IBS\n");
  487. return -ENODEV;
  488. }
  489. return 0;
  490. }
  491. /*
  492. * This runs only on the current cpu. We try to find an LVT offset and
  493. * setup the local APIC. For this we must disable preemption. On
  494. * success we initialize all nodes with this offset. This updates then
  495. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  496. * the IBS interrupt vector is called from op_amd_setup_ctrs()/op_-
  497. * amd_cpu_shutdown() using the new offset.
  498. */
  499. static int force_ibs_eilvt_setup(void)
  500. {
  501. int offset;
  502. int ret;
  503. preempt_disable();
  504. /* find the next free available EILVT entry, skip offset 0 */
  505. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  506. if (get_eilvt(offset))
  507. break;
  508. }
  509. preempt_enable();
  510. if (offset == APIC_EILVT_NR_MAX) {
  511. printk(KERN_DEBUG "No EILVT entry available\n");
  512. return -EBUSY;
  513. }
  514. ret = setup_ibs_ctl(offset);
  515. if (ret)
  516. goto out;
  517. if (!ibs_eilvt_valid()) {
  518. ret = -EFAULT;
  519. goto out;
  520. }
  521. pr_err(FW_BUG "using offset %d for IBS interrupts\n", offset);
  522. pr_err(FW_BUG "workaround enabled for IBS LVT offset\n");
  523. return 0;
  524. out:
  525. preempt_disable();
  526. put_eilvt(offset);
  527. preempt_enable();
  528. return ret;
  529. }
  530. /*
  531. * check and reserve APIC extended interrupt LVT offset for IBS if
  532. * available
  533. */
  534. static void init_ibs(void)
  535. {
  536. ibs_caps = get_ibs_caps();
  537. if (!ibs_caps)
  538. return;
  539. if (ibs_eilvt_valid())
  540. goto out;
  541. if (!force_ibs_eilvt_setup())
  542. goto out;
  543. /* Failed to setup ibs */
  544. ibs_caps = 0;
  545. return;
  546. out:
  547. printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n", ibs_caps);
  548. }
  549. static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
  550. static int setup_ibs_files(struct super_block *sb, struct dentry *root)
  551. {
  552. struct dentry *dir;
  553. int ret = 0;
  554. /* architecture specific files */
  555. if (create_arch_files)
  556. ret = create_arch_files(sb, root);
  557. if (ret)
  558. return ret;
  559. if (!ibs_caps)
  560. return ret;
  561. /* model specific files */
  562. /* setup some reasonable defaults */
  563. memset(&ibs_config, 0, sizeof(ibs_config));
  564. ibs_config.max_cnt_fetch = 250000;
  565. ibs_config.max_cnt_op = 250000;
  566. if (ibs_caps & IBS_CAPS_FETCHSAM) {
  567. dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
  568. oprofilefs_create_ulong(sb, dir, "enable",
  569. &ibs_config.fetch_enabled);
  570. oprofilefs_create_ulong(sb, dir, "max_count",
  571. &ibs_config.max_cnt_fetch);
  572. oprofilefs_create_ulong(sb, dir, "rand_enable",
  573. &ibs_config.rand_en);
  574. }
  575. if (ibs_caps & IBS_CAPS_OPSAM) {
  576. dir = oprofilefs_mkdir(sb, root, "ibs_op");
  577. oprofilefs_create_ulong(sb, dir, "enable",
  578. &ibs_config.op_enabled);
  579. oprofilefs_create_ulong(sb, dir, "max_count",
  580. &ibs_config.max_cnt_op);
  581. if (ibs_caps & IBS_CAPS_OPCNT)
  582. oprofilefs_create_ulong(sb, dir, "dispatched_ops",
  583. &ibs_config.dispatched_ops);
  584. if (ibs_caps & IBS_CAPS_BRNTRGT)
  585. oprofilefs_create_ulong(sb, dir, "branch_target",
  586. &ibs_config.branch_target);
  587. }
  588. return 0;
  589. }
  590. struct op_x86_model_spec op_amd_spec;
  591. static int op_amd_init(struct oprofile_operations *ops)
  592. {
  593. init_ibs();
  594. create_arch_files = ops->create_files;
  595. ops->create_files = setup_ibs_files;
  596. if (boot_cpu_data.x86 == 0x15) {
  597. num_counters = AMD64_NUM_COUNTERS_F15H;
  598. } else {
  599. num_counters = AMD64_NUM_COUNTERS;
  600. }
  601. op_amd_spec.num_counters = num_counters;
  602. op_amd_spec.num_controls = num_counters;
  603. op_amd_spec.num_virt_counters = max(num_counters, NUM_VIRT_COUNTERS);
  604. return 0;
  605. }
  606. struct op_x86_model_spec op_amd_spec = {
  607. /* num_counters/num_controls filled in at runtime */
  608. .reserved = MSR_AMD_EVENTSEL_RESERVED,
  609. .event_mask = OP_EVENT_MASK,
  610. .init = op_amd_init,
  611. .fill_in_addresses = &op_amd_fill_in_addresses,
  612. .setup_ctrs = &op_amd_setup_ctrs,
  613. .cpu_down = &op_amd_cpu_shutdown,
  614. .check_ctrs = &op_amd_check_ctrs,
  615. .start = &op_amd_start,
  616. .stop = &op_amd_stop,
  617. .shutdown = &op_amd_shutdown,
  618. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  619. .switch_ctrl = &op_mux_switch_ctrl,
  620. #endif
  621. };