op_model_amd.c 17 KB

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