op_model_amd.c 16 KB

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