op_model_amd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 get_eilvt(int offset)
  274. {
  275. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  276. }
  277. static inline int put_eilvt(int offset)
  278. {
  279. return !setup_APIC_eilvt(offset, 0, 0, 1);
  280. }
  281. static inline int ibs_eilvt_valid(void)
  282. {
  283. int offset;
  284. u64 val;
  285. int valid = 0;
  286. preempt_disable();
  287. rdmsrl(MSR_AMD64_IBSCTL, val);
  288. offset = val & IBSCTL_LVT_OFFSET_MASK;
  289. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  290. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  291. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  292. goto out;
  293. }
  294. if (!get_eilvt(offset)) {
  295. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  296. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  297. goto out;
  298. }
  299. valid = 1;
  300. out:
  301. preempt_enable();
  302. return valid;
  303. }
  304. static inline int get_ibs_offset(void)
  305. {
  306. u64 val;
  307. rdmsrl(MSR_AMD64_IBSCTL, val);
  308. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  309. return -EINVAL;
  310. return val & IBSCTL_LVT_OFFSET_MASK;
  311. }
  312. static void setup_APIC_ibs(void)
  313. {
  314. int offset;
  315. offset = get_ibs_offset();
  316. if (offset < 0)
  317. goto failed;
  318. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  319. return;
  320. failed:
  321. pr_warn("oprofile: IBS APIC setup failed on cpu #%d\n",
  322. smp_processor_id());
  323. }
  324. static void clear_APIC_ibs(void)
  325. {
  326. int offset;
  327. offset = get_ibs_offset();
  328. if (offset >= 0)
  329. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  330. }
  331. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  332. static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
  333. struct op_msrs const * const msrs)
  334. {
  335. u64 val;
  336. int i;
  337. /* enable active counters */
  338. for (i = 0; i < num_counters; ++i) {
  339. int virt = op_x86_phys_to_virt(i);
  340. if (!reset_value[virt])
  341. continue;
  342. rdmsrl(msrs->controls[i].addr, val);
  343. val &= model->reserved;
  344. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  345. wrmsrl(msrs->controls[i].addr, val);
  346. }
  347. }
  348. #endif
  349. /* functions for op_amd_spec */
  350. static void op_amd_shutdown(struct op_msrs const * const msrs)
  351. {
  352. int i;
  353. for (i = 0; i < num_counters; ++i) {
  354. if (!msrs->counters[i].addr)
  355. continue;
  356. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  357. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  358. }
  359. }
  360. static int op_amd_fill_in_addresses(struct op_msrs * const msrs)
  361. {
  362. int i;
  363. for (i = 0; i < num_counters; i++) {
  364. if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  365. goto fail;
  366. if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) {
  367. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  368. goto fail;
  369. }
  370. /* both registers must be reserved */
  371. if (num_counters == NUM_COUNTERS_F15H) {
  372. msrs->counters[i].addr = MSR_F15H_PERF_CTR + (i << 1);
  373. msrs->controls[i].addr = MSR_F15H_PERF_CTL + (i << 1);
  374. } else {
  375. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  376. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  377. }
  378. continue;
  379. fail:
  380. if (!counter_config[i].enabled)
  381. continue;
  382. op_x86_warn_reserved(i);
  383. op_amd_shutdown(msrs);
  384. return -EBUSY;
  385. }
  386. return 0;
  387. }
  388. static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
  389. struct op_msrs const * const msrs)
  390. {
  391. u64 val;
  392. int i;
  393. /* setup reset_value */
  394. for (i = 0; i < OP_MAX_COUNTER; ++i) {
  395. if (counter_config[i].enabled
  396. && msrs->counters[op_x86_virt_to_phys(i)].addr)
  397. reset_value[i] = counter_config[i].count;
  398. else
  399. reset_value[i] = 0;
  400. }
  401. /* clear all counters */
  402. for (i = 0; i < num_counters; ++i) {
  403. if (!msrs->controls[i].addr)
  404. continue;
  405. rdmsrl(msrs->controls[i].addr, val);
  406. if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
  407. op_x86_warn_in_use(i);
  408. val &= model->reserved;
  409. wrmsrl(msrs->controls[i].addr, val);
  410. /*
  411. * avoid a false detection of ctr overflows in NMI
  412. * handler
  413. */
  414. wrmsrl(msrs->counters[i].addr, -1LL);
  415. }
  416. /* enable active counters */
  417. for (i = 0; i < num_counters; ++i) {
  418. int virt = op_x86_phys_to_virt(i);
  419. if (!reset_value[virt])
  420. continue;
  421. /* setup counter registers */
  422. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  423. /* setup control registers */
  424. rdmsrl(msrs->controls[i].addr, val);
  425. val &= model->reserved;
  426. val |= op_x86_get_ctrl(model, &counter_config[virt]);
  427. wrmsrl(msrs->controls[i].addr, val);
  428. }
  429. if (ibs_caps)
  430. setup_APIC_ibs();
  431. }
  432. static void op_amd_cpu_shutdown(void)
  433. {
  434. if (ibs_caps)
  435. clear_APIC_ibs();
  436. }
  437. static int op_amd_check_ctrs(struct pt_regs * const regs,
  438. struct op_msrs const * const msrs)
  439. {
  440. u64 val;
  441. int i;
  442. for (i = 0; i < num_counters; ++i) {
  443. int virt = op_x86_phys_to_virt(i);
  444. if (!reset_value[virt])
  445. continue;
  446. rdmsrl(msrs->counters[i].addr, val);
  447. /* bit is clear if overflowed: */
  448. if (val & OP_CTR_OVERFLOW)
  449. continue;
  450. oprofile_add_sample(regs, virt);
  451. wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
  452. }
  453. op_amd_handle_ibs(regs, msrs);
  454. /* See op_model_ppro.c */
  455. return 1;
  456. }
  457. static void op_amd_start(struct op_msrs const * const msrs)
  458. {
  459. u64 val;
  460. int i;
  461. for (i = 0; i < num_counters; ++i) {
  462. if (!reset_value[op_x86_phys_to_virt(i)])
  463. continue;
  464. rdmsrl(msrs->controls[i].addr, val);
  465. val |= ARCH_PERFMON_EVENTSEL_ENABLE;
  466. wrmsrl(msrs->controls[i].addr, val);
  467. }
  468. op_amd_start_ibs();
  469. }
  470. static void op_amd_stop(struct op_msrs const * const msrs)
  471. {
  472. u64 val;
  473. int i;
  474. /*
  475. * Subtle: stop on all counters to avoid race with setting our
  476. * pm callback
  477. */
  478. for (i = 0; i < num_counters; ++i) {
  479. if (!reset_value[op_x86_phys_to_virt(i)])
  480. continue;
  481. rdmsrl(msrs->controls[i].addr, val);
  482. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  483. wrmsrl(msrs->controls[i].addr, val);
  484. }
  485. op_amd_stop_ibs();
  486. }
  487. static int setup_ibs_ctl(int ibs_eilvt_off)
  488. {
  489. struct pci_dev *cpu_cfg;
  490. int nodes;
  491. u32 value = 0;
  492. nodes = 0;
  493. cpu_cfg = NULL;
  494. do {
  495. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  496. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  497. cpu_cfg);
  498. if (!cpu_cfg)
  499. break;
  500. ++nodes;
  501. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  502. | IBSCTL_LVT_OFFSET_VALID);
  503. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  504. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  505. pci_dev_put(cpu_cfg);
  506. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  507. "IBSCTL = 0x%08x\n", value);
  508. return -EINVAL;
  509. }
  510. } while (1);
  511. if (!nodes) {
  512. printk(KERN_DEBUG "No CPU node configured for IBS\n");
  513. return -ENODEV;
  514. }
  515. return 0;
  516. }
  517. /*
  518. * This runs only on the current cpu. We try to find an LVT offset and
  519. * setup the local APIC. For this we must disable preemption. On
  520. * success we initialize all nodes with this offset. This updates then
  521. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  522. * the IBS interrupt vector is called from op_amd_setup_ctrs()/op_-
  523. * amd_cpu_shutdown() using the new offset.
  524. */
  525. static int force_ibs_eilvt_setup(void)
  526. {
  527. int offset;
  528. int ret;
  529. preempt_disable();
  530. /* find the next free available EILVT entry, skip offset 0 */
  531. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  532. if (get_eilvt(offset))
  533. break;
  534. }
  535. preempt_enable();
  536. if (offset == APIC_EILVT_NR_MAX) {
  537. printk(KERN_DEBUG "No EILVT entry available\n");
  538. return -EBUSY;
  539. }
  540. ret = setup_ibs_ctl(offset);
  541. if (ret)
  542. goto out;
  543. if (!ibs_eilvt_valid()) {
  544. ret = -EFAULT;
  545. goto out;
  546. }
  547. pr_err(FW_BUG "using offset %d for IBS interrupts\n", offset);
  548. pr_err(FW_BUG "workaround enabled for IBS LVT offset\n");
  549. return 0;
  550. out:
  551. preempt_disable();
  552. put_eilvt(offset);
  553. preempt_enable();
  554. return ret;
  555. }
  556. /*
  557. * check and reserve APIC extended interrupt LVT offset for IBS if
  558. * available
  559. */
  560. static void init_ibs(void)
  561. {
  562. ibs_caps = get_ibs_caps();
  563. if (!ibs_caps)
  564. return;
  565. if (ibs_eilvt_valid())
  566. goto out;
  567. if (!force_ibs_eilvt_setup())
  568. goto out;
  569. /* Failed to setup ibs */
  570. ibs_caps = 0;
  571. return;
  572. out:
  573. printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n", ibs_caps);
  574. }
  575. static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
  576. static int setup_ibs_files(struct super_block *sb, struct dentry *root)
  577. {
  578. struct dentry *dir;
  579. int ret = 0;
  580. /* architecture specific files */
  581. if (create_arch_files)
  582. ret = create_arch_files(sb, root);
  583. if (ret)
  584. return ret;
  585. if (!ibs_caps)
  586. return ret;
  587. /* model specific files */
  588. /* setup some reasonable defaults */
  589. memset(&ibs_config, 0, sizeof(ibs_config));
  590. ibs_config.max_cnt_fetch = 250000;
  591. ibs_config.max_cnt_op = 250000;
  592. if (ibs_caps & IBS_CAPS_FETCHSAM) {
  593. dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
  594. oprofilefs_create_ulong(sb, dir, "enable",
  595. &ibs_config.fetch_enabled);
  596. oprofilefs_create_ulong(sb, dir, "max_count",
  597. &ibs_config.max_cnt_fetch);
  598. oprofilefs_create_ulong(sb, dir, "rand_enable",
  599. &ibs_config.rand_en);
  600. }
  601. if (ibs_caps & IBS_CAPS_OPSAM) {
  602. dir = oprofilefs_mkdir(sb, root, "ibs_op");
  603. oprofilefs_create_ulong(sb, dir, "enable",
  604. &ibs_config.op_enabled);
  605. oprofilefs_create_ulong(sb, dir, "max_count",
  606. &ibs_config.max_cnt_op);
  607. if (ibs_caps & IBS_CAPS_OPCNT)
  608. oprofilefs_create_ulong(sb, dir, "dispatched_ops",
  609. &ibs_config.dispatched_ops);
  610. if (ibs_caps & IBS_CAPS_BRNTRGT)
  611. oprofilefs_create_ulong(sb, dir, "branch_target",
  612. &ibs_config.branch_target);
  613. }
  614. return 0;
  615. }
  616. struct op_x86_model_spec op_amd_spec;
  617. static int op_amd_init(struct oprofile_operations *ops)
  618. {
  619. init_ibs();
  620. create_arch_files = ops->create_files;
  621. ops->create_files = setup_ibs_files;
  622. if (boot_cpu_data.x86 == 0x15) {
  623. num_counters = NUM_COUNTERS_F15H;
  624. } else {
  625. num_counters = NUM_COUNTERS;
  626. }
  627. op_amd_spec.num_counters = num_counters;
  628. op_amd_spec.num_controls = num_counters;
  629. op_amd_spec.num_virt_counters = max(num_counters, NUM_VIRT_COUNTERS);
  630. return 0;
  631. }
  632. struct op_x86_model_spec op_amd_spec = {
  633. /* num_counters/num_controls filled in at runtime */
  634. .reserved = MSR_AMD_EVENTSEL_RESERVED,
  635. .event_mask = OP_EVENT_MASK,
  636. .init = op_amd_init,
  637. .fill_in_addresses = &op_amd_fill_in_addresses,
  638. .setup_ctrs = &op_amd_setup_ctrs,
  639. .cpu_down = &op_amd_cpu_shutdown,
  640. .check_ctrs = &op_amd_check_ctrs,
  641. .start = &op_amd_start,
  642. .stop = &op_amd_stop,
  643. .shutdown = &op_amd_shutdown,
  644. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  645. .switch_ctrl = &op_mux_switch_ctrl,
  646. #endif
  647. };