perf_event_amd_ibs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Performance events - AMD IBS
  3. *
  4. * Copyright (C) 2011 Advanced Micro Devices, Inc., Robert Richter
  5. *
  6. * For licencing details see kernel-base/COPYING
  7. */
  8. #include <linux/perf_event.h>
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/ptrace.h>
  12. #include <asm/apic.h>
  13. static u32 ibs_caps;
  14. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
  15. #include <linux/kprobes.h>
  16. #include <linux/hardirq.h>
  17. #include <asm/nmi.h>
  18. #define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)
  19. #define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT
  20. enum ibs_states {
  21. IBS_ENABLED = 0,
  22. IBS_STARTED = 1,
  23. IBS_STOPPING = 2,
  24. IBS_MAX_STATES,
  25. };
  26. struct cpu_perf_ibs {
  27. struct perf_event *event;
  28. unsigned long state[BITS_TO_LONGS(IBS_MAX_STATES)];
  29. };
  30. struct perf_ibs {
  31. struct pmu pmu;
  32. unsigned int msr;
  33. u64 config_mask;
  34. u64 cnt_mask;
  35. u64 enable_mask;
  36. u64 valid_mask;
  37. u64 max_period;
  38. unsigned long offset_mask[1];
  39. int offset_max;
  40. struct cpu_perf_ibs __percpu *pcpu;
  41. u64 (*get_count)(u64 config);
  42. };
  43. struct perf_ibs_data {
  44. u32 size;
  45. union {
  46. u32 data[0]; /* data buffer starts here */
  47. u32 caps;
  48. };
  49. u64 regs[MSR_AMD64_IBS_REG_COUNT_MAX];
  50. };
  51. static int
  52. perf_event_set_period(struct hw_perf_event *hwc, u64 min, u64 max, u64 *hw_period)
  53. {
  54. s64 left = local64_read(&hwc->period_left);
  55. s64 period = hwc->sample_period;
  56. int overflow = 0;
  57. /*
  58. * If we are way outside a reasonable range then just skip forward:
  59. */
  60. if (unlikely(left <= -period)) {
  61. left = period;
  62. local64_set(&hwc->period_left, left);
  63. hwc->last_period = period;
  64. overflow = 1;
  65. }
  66. if (unlikely(left < (s64)min)) {
  67. left += period;
  68. local64_set(&hwc->period_left, left);
  69. hwc->last_period = period;
  70. overflow = 1;
  71. }
  72. /*
  73. * If the hw period that triggers the sw overflow is too short
  74. * we might hit the irq handler. This biases the results.
  75. * Thus we shorten the next-to-last period and set the last
  76. * period to the max period.
  77. */
  78. if (left > max) {
  79. left -= max;
  80. if (left > max)
  81. left = max;
  82. else if (left < min)
  83. left = min;
  84. }
  85. *hw_period = (u64)left;
  86. return overflow;
  87. }
  88. static int
  89. perf_event_try_update(struct perf_event *event, u64 new_raw_count, int width)
  90. {
  91. struct hw_perf_event *hwc = &event->hw;
  92. int shift = 64 - width;
  93. u64 prev_raw_count;
  94. u64 delta;
  95. /*
  96. * Careful: an NMI might modify the previous event value.
  97. *
  98. * Our tactic to handle this is to first atomically read and
  99. * exchange a new raw count - then add that new-prev delta
  100. * count to the generic event atomically:
  101. */
  102. prev_raw_count = local64_read(&hwc->prev_count);
  103. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  104. new_raw_count) != prev_raw_count)
  105. return 0;
  106. /*
  107. * Now we have the new raw value and have updated the prev
  108. * timestamp already. We can now calculate the elapsed delta
  109. * (event-)time and add that to the generic event.
  110. *
  111. * Careful, not all hw sign-extends above the physical width
  112. * of the count.
  113. */
  114. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  115. delta >>= shift;
  116. local64_add(delta, &event->count);
  117. local64_sub(delta, &hwc->period_left);
  118. return 1;
  119. }
  120. static struct perf_ibs perf_ibs_fetch;
  121. static struct perf_ibs perf_ibs_op;
  122. static struct perf_ibs *get_ibs_pmu(int type)
  123. {
  124. if (perf_ibs_fetch.pmu.type == type)
  125. return &perf_ibs_fetch;
  126. if (perf_ibs_op.pmu.type == type)
  127. return &perf_ibs_op;
  128. return NULL;
  129. }
  130. /*
  131. * Use IBS for precise event sampling:
  132. *
  133. * perf record -a -e cpu-cycles:p ... # use ibs op counting cycle count
  134. * perf record -a -e r076:p ... # same as -e cpu-cycles:p
  135. * perf record -a -e r0C1:p ... # use ibs op counting micro-ops
  136. *
  137. * IbsOpCntCtl (bit 19) of IBS Execution Control Register (IbsOpCtl,
  138. * MSRC001_1033) is used to select either cycle or micro-ops counting
  139. * mode.
  140. *
  141. * The rip of IBS samples has skid 0. Thus, IBS supports precise
  142. * levels 1 and 2 and the PERF_EFLAGS_EXACT is set. In rare cases the
  143. * rip is invalid when IBS was not able to record the rip correctly.
  144. * We clear PERF_EFLAGS_EXACT and take the rip from pt_regs then.
  145. *
  146. */
  147. static int perf_ibs_precise_event(struct perf_event *event, u64 *config)
  148. {
  149. switch (event->attr.precise_ip) {
  150. case 0:
  151. return -ENOENT;
  152. case 1:
  153. case 2:
  154. break;
  155. default:
  156. return -EOPNOTSUPP;
  157. }
  158. switch (event->attr.type) {
  159. case PERF_TYPE_HARDWARE:
  160. switch (event->attr.config) {
  161. case PERF_COUNT_HW_CPU_CYCLES:
  162. *config = 0;
  163. return 0;
  164. }
  165. break;
  166. case PERF_TYPE_RAW:
  167. switch (event->attr.config) {
  168. case 0x0076:
  169. *config = 0;
  170. return 0;
  171. case 0x00C1:
  172. *config = IBS_OP_CNT_CTL;
  173. return 0;
  174. }
  175. break;
  176. default:
  177. return -ENOENT;
  178. }
  179. return -EOPNOTSUPP;
  180. }
  181. static int perf_ibs_init(struct perf_event *event)
  182. {
  183. struct hw_perf_event *hwc = &event->hw;
  184. struct perf_ibs *perf_ibs;
  185. u64 max_cnt, config;
  186. int ret;
  187. perf_ibs = get_ibs_pmu(event->attr.type);
  188. if (perf_ibs) {
  189. config = event->attr.config;
  190. } else {
  191. perf_ibs = &perf_ibs_op;
  192. ret = perf_ibs_precise_event(event, &config);
  193. if (ret)
  194. return ret;
  195. }
  196. if (event->pmu != &perf_ibs->pmu)
  197. return -ENOENT;
  198. if (config & ~perf_ibs->config_mask)
  199. return -EINVAL;
  200. if (hwc->sample_period) {
  201. if (config & perf_ibs->cnt_mask)
  202. /* raw max_cnt may not be set */
  203. return -EINVAL;
  204. if (!event->attr.sample_freq && hwc->sample_period & 0x0f)
  205. /*
  206. * lower 4 bits can not be set in ibs max cnt,
  207. * but allowing it in case we adjust the
  208. * sample period to set a frequency.
  209. */
  210. return -EINVAL;
  211. hwc->sample_period &= ~0x0FULL;
  212. if (!hwc->sample_period)
  213. hwc->sample_period = 0x10;
  214. } else {
  215. max_cnt = config & perf_ibs->cnt_mask;
  216. config &= ~perf_ibs->cnt_mask;
  217. event->attr.sample_period = max_cnt << 4;
  218. hwc->sample_period = event->attr.sample_period;
  219. }
  220. if (!hwc->sample_period)
  221. return -EINVAL;
  222. /*
  223. * If we modify hwc->sample_period, we also need to update
  224. * hwc->last_period and hwc->period_left.
  225. */
  226. hwc->last_period = hwc->sample_period;
  227. local64_set(&hwc->period_left, hwc->sample_period);
  228. hwc->config_base = perf_ibs->msr;
  229. hwc->config = config;
  230. return 0;
  231. }
  232. static int perf_ibs_set_period(struct perf_ibs *perf_ibs,
  233. struct hw_perf_event *hwc, u64 *period)
  234. {
  235. int overflow;
  236. /* ignore lower 4 bits in min count: */
  237. overflow = perf_event_set_period(hwc, 1<<4, perf_ibs->max_period, period);
  238. local64_set(&hwc->prev_count, 0);
  239. return overflow;
  240. }
  241. static u64 get_ibs_fetch_count(u64 config)
  242. {
  243. return (config & IBS_FETCH_CNT) >> 12;
  244. }
  245. static u64 get_ibs_op_count(u64 config)
  246. {
  247. u64 count = 0;
  248. if (config & IBS_OP_VAL)
  249. count += (config & IBS_OP_MAX_CNT) << 4; /* cnt rolled over */
  250. if (ibs_caps & IBS_CAPS_RDWROPCNT)
  251. count += (config & IBS_OP_CUR_CNT) >> 32;
  252. return count;
  253. }
  254. static void
  255. perf_ibs_event_update(struct perf_ibs *perf_ibs, struct perf_event *event,
  256. u64 *config)
  257. {
  258. u64 count = perf_ibs->get_count(*config);
  259. /*
  260. * Set width to 64 since we do not overflow on max width but
  261. * instead on max count. In perf_ibs_set_period() we clear
  262. * prev count manually on overflow.
  263. */
  264. while (!perf_event_try_update(event, count, 64)) {
  265. rdmsrl(event->hw.config_base, *config);
  266. count = perf_ibs->get_count(*config);
  267. }
  268. }
  269. static inline void perf_ibs_enable_event(struct perf_ibs *perf_ibs,
  270. struct hw_perf_event *hwc, u64 config)
  271. {
  272. wrmsrl(hwc->config_base, hwc->config | config | perf_ibs->enable_mask);
  273. }
  274. /*
  275. * Erratum #420 Instruction-Based Sampling Engine May Generate
  276. * Interrupt that Cannot Be Cleared:
  277. *
  278. * Must clear counter mask first, then clear the enable bit. See
  279. * Revision Guide for AMD Family 10h Processors, Publication #41322.
  280. */
  281. static inline void perf_ibs_disable_event(struct perf_ibs *perf_ibs,
  282. struct hw_perf_event *hwc, u64 config)
  283. {
  284. config &= ~perf_ibs->cnt_mask;
  285. wrmsrl(hwc->config_base, config);
  286. config &= ~perf_ibs->enable_mask;
  287. wrmsrl(hwc->config_base, config);
  288. }
  289. /*
  290. * We cannot restore the ibs pmu state, so we always needs to update
  291. * the event while stopping it and then reset the state when starting
  292. * again. Thus, ignoring PERF_EF_RELOAD and PERF_EF_UPDATE flags in
  293. * perf_ibs_start()/perf_ibs_stop() and instead always do it.
  294. */
  295. static void perf_ibs_start(struct perf_event *event, int flags)
  296. {
  297. struct hw_perf_event *hwc = &event->hw;
  298. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  299. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  300. u64 period;
  301. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  302. return;
  303. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  304. hwc->state = 0;
  305. perf_ibs_set_period(perf_ibs, hwc, &period);
  306. set_bit(IBS_STARTED, pcpu->state);
  307. perf_ibs_enable_event(perf_ibs, hwc, period >> 4);
  308. perf_event_update_userpage(event);
  309. }
  310. static void perf_ibs_stop(struct perf_event *event, int flags)
  311. {
  312. struct hw_perf_event *hwc = &event->hw;
  313. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  314. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  315. u64 config;
  316. int stopping;
  317. stopping = test_and_clear_bit(IBS_STARTED, pcpu->state);
  318. if (!stopping && (hwc->state & PERF_HES_UPTODATE))
  319. return;
  320. rdmsrl(hwc->config_base, config);
  321. if (stopping) {
  322. set_bit(IBS_STOPPING, pcpu->state);
  323. perf_ibs_disable_event(perf_ibs, hwc, config);
  324. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  325. hwc->state |= PERF_HES_STOPPED;
  326. }
  327. if (hwc->state & PERF_HES_UPTODATE)
  328. return;
  329. /*
  330. * Clear valid bit to not count rollovers on update, rollovers
  331. * are only updated in the irq handler.
  332. */
  333. config &= ~perf_ibs->valid_mask;
  334. perf_ibs_event_update(perf_ibs, event, &config);
  335. hwc->state |= PERF_HES_UPTODATE;
  336. }
  337. static int perf_ibs_add(struct perf_event *event, int flags)
  338. {
  339. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  340. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  341. if (test_and_set_bit(IBS_ENABLED, pcpu->state))
  342. return -ENOSPC;
  343. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  344. pcpu->event = event;
  345. if (flags & PERF_EF_START)
  346. perf_ibs_start(event, PERF_EF_RELOAD);
  347. return 0;
  348. }
  349. static void perf_ibs_del(struct perf_event *event, int flags)
  350. {
  351. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  352. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  353. if (!test_and_clear_bit(IBS_ENABLED, pcpu->state))
  354. return;
  355. perf_ibs_stop(event, PERF_EF_UPDATE);
  356. pcpu->event = NULL;
  357. perf_event_update_userpage(event);
  358. }
  359. static void perf_ibs_read(struct perf_event *event) { }
  360. static struct perf_ibs perf_ibs_fetch = {
  361. .pmu = {
  362. .task_ctx_nr = perf_invalid_context,
  363. .event_init = perf_ibs_init,
  364. .add = perf_ibs_add,
  365. .del = perf_ibs_del,
  366. .start = perf_ibs_start,
  367. .stop = perf_ibs_stop,
  368. .read = perf_ibs_read,
  369. },
  370. .msr = MSR_AMD64_IBSFETCHCTL,
  371. .config_mask = IBS_FETCH_CONFIG_MASK,
  372. .cnt_mask = IBS_FETCH_MAX_CNT,
  373. .enable_mask = IBS_FETCH_ENABLE,
  374. .valid_mask = IBS_FETCH_VAL,
  375. .max_period = IBS_FETCH_MAX_CNT << 4,
  376. .offset_mask = { MSR_AMD64_IBSFETCH_REG_MASK },
  377. .offset_max = MSR_AMD64_IBSFETCH_REG_COUNT,
  378. .get_count = get_ibs_fetch_count,
  379. };
  380. static struct perf_ibs perf_ibs_op = {
  381. .pmu = {
  382. .task_ctx_nr = perf_invalid_context,
  383. .event_init = perf_ibs_init,
  384. .add = perf_ibs_add,
  385. .del = perf_ibs_del,
  386. .start = perf_ibs_start,
  387. .stop = perf_ibs_stop,
  388. .read = perf_ibs_read,
  389. },
  390. .msr = MSR_AMD64_IBSOPCTL,
  391. .config_mask = IBS_OP_CONFIG_MASK,
  392. .cnt_mask = IBS_OP_MAX_CNT,
  393. .enable_mask = IBS_OP_ENABLE,
  394. .valid_mask = IBS_OP_VAL,
  395. .max_period = IBS_OP_MAX_CNT << 4,
  396. .offset_mask = { MSR_AMD64_IBSOP_REG_MASK },
  397. .offset_max = MSR_AMD64_IBSOP_REG_COUNT,
  398. .get_count = get_ibs_op_count,
  399. };
  400. static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
  401. {
  402. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  403. struct perf_event *event = pcpu->event;
  404. struct hw_perf_event *hwc = &event->hw;
  405. struct perf_sample_data data;
  406. struct perf_raw_record raw;
  407. struct pt_regs regs;
  408. struct perf_ibs_data ibs_data;
  409. int offset, size, check_rip, offset_max, throttle = 0;
  410. unsigned int msr;
  411. u64 *buf, *config, period;
  412. if (!test_bit(IBS_STARTED, pcpu->state)) {
  413. /*
  414. * Catch spurious interrupts after stopping IBS: After
  415. * disabling IBS there could be still incomming NMIs
  416. * with samples that even have the valid bit cleared.
  417. * Mark all this NMIs as handled.
  418. */
  419. return test_and_clear_bit(IBS_STOPPING, pcpu->state) ? 1 : 0;
  420. }
  421. msr = hwc->config_base;
  422. buf = ibs_data.regs;
  423. rdmsrl(msr, *buf);
  424. if (!(*buf++ & perf_ibs->valid_mask))
  425. return 0;
  426. config = &ibs_data.regs[0];
  427. perf_ibs_event_update(perf_ibs, event, config);
  428. perf_sample_data_init(&data, 0, hwc->last_period);
  429. if (!perf_ibs_set_period(perf_ibs, hwc, &period))
  430. goto out; /* no sw counter overflow */
  431. ibs_data.caps = ibs_caps;
  432. size = 1;
  433. offset = 1;
  434. check_rip = (perf_ibs == &perf_ibs_op && (ibs_caps & IBS_CAPS_RIPINVALIDCHK));
  435. if (event->attr.sample_type & PERF_SAMPLE_RAW)
  436. offset_max = perf_ibs->offset_max;
  437. else if (check_rip)
  438. offset_max = 2;
  439. else
  440. offset_max = 1;
  441. do {
  442. rdmsrl(msr + offset, *buf++);
  443. size++;
  444. offset = find_next_bit(perf_ibs->offset_mask,
  445. perf_ibs->offset_max,
  446. offset + 1);
  447. } while (offset < offset_max);
  448. ibs_data.size = sizeof(u64) * size;
  449. regs = *iregs;
  450. if (check_rip && (ibs_data.regs[2] & IBS_RIP_INVALID)) {
  451. regs.flags &= ~PERF_EFLAGS_EXACT;
  452. } else {
  453. instruction_pointer_set(&regs, ibs_data.regs[1]);
  454. regs.flags |= PERF_EFLAGS_EXACT;
  455. }
  456. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  457. raw.size = sizeof(u32) + ibs_data.size;
  458. raw.data = ibs_data.data;
  459. data.raw = &raw;
  460. }
  461. throttle = perf_event_overflow(event, &data, &regs);
  462. out:
  463. if (throttle)
  464. perf_ibs_disable_event(perf_ibs, hwc, *config);
  465. else
  466. perf_ibs_enable_event(perf_ibs, hwc, period >> 4);
  467. perf_event_update_userpage(event);
  468. return 1;
  469. }
  470. static int __kprobes
  471. perf_ibs_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  472. {
  473. int handled = 0;
  474. handled += perf_ibs_handle_irq(&perf_ibs_fetch, regs);
  475. handled += perf_ibs_handle_irq(&perf_ibs_op, regs);
  476. if (handled)
  477. inc_irq_stat(apic_perf_irqs);
  478. return handled;
  479. }
  480. static __init int perf_ibs_pmu_init(struct perf_ibs *perf_ibs, char *name)
  481. {
  482. struct cpu_perf_ibs __percpu *pcpu;
  483. int ret;
  484. pcpu = alloc_percpu(struct cpu_perf_ibs);
  485. if (!pcpu)
  486. return -ENOMEM;
  487. perf_ibs->pcpu = pcpu;
  488. ret = perf_pmu_register(&perf_ibs->pmu, name, -1);
  489. if (ret) {
  490. perf_ibs->pcpu = NULL;
  491. free_percpu(pcpu);
  492. }
  493. return ret;
  494. }
  495. static __init int perf_event_ibs_init(void)
  496. {
  497. if (!ibs_caps)
  498. return -ENODEV; /* ibs not supported by the cpu */
  499. perf_ibs_pmu_init(&perf_ibs_fetch, "ibs_fetch");
  500. if (ibs_caps & IBS_CAPS_OPCNT)
  501. perf_ibs_op.config_mask |= IBS_OP_CNT_CTL;
  502. perf_ibs_pmu_init(&perf_ibs_op, "ibs_op");
  503. register_nmi_handler(NMI_LOCAL, perf_ibs_nmi_handler, 0, "perf_ibs");
  504. printk(KERN_INFO "perf: AMD IBS detected (0x%08x)\n", ibs_caps);
  505. return 0;
  506. }
  507. #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
  508. static __init int perf_event_ibs_init(void) { return 0; }
  509. #endif
  510. /* IBS - apic initialization, for perf and oprofile */
  511. static __init u32 __get_ibs_caps(void)
  512. {
  513. u32 caps;
  514. unsigned int max_level;
  515. if (!boot_cpu_has(X86_FEATURE_IBS))
  516. return 0;
  517. /* check IBS cpuid feature flags */
  518. max_level = cpuid_eax(0x80000000);
  519. if (max_level < IBS_CPUID_FEATURES)
  520. return IBS_CAPS_DEFAULT;
  521. caps = cpuid_eax(IBS_CPUID_FEATURES);
  522. if (!(caps & IBS_CAPS_AVAIL))
  523. /* cpuid flags not valid */
  524. return IBS_CAPS_DEFAULT;
  525. return caps;
  526. }
  527. u32 get_ibs_caps(void)
  528. {
  529. return ibs_caps;
  530. }
  531. EXPORT_SYMBOL(get_ibs_caps);
  532. static inline int get_eilvt(int offset)
  533. {
  534. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  535. }
  536. static inline int put_eilvt(int offset)
  537. {
  538. return !setup_APIC_eilvt(offset, 0, 0, 1);
  539. }
  540. /*
  541. * Check and reserve APIC extended interrupt LVT offset for IBS if available.
  542. */
  543. static inline int ibs_eilvt_valid(void)
  544. {
  545. int offset;
  546. u64 val;
  547. int valid = 0;
  548. preempt_disable();
  549. rdmsrl(MSR_AMD64_IBSCTL, val);
  550. offset = val & IBSCTL_LVT_OFFSET_MASK;
  551. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  552. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  553. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  554. goto out;
  555. }
  556. if (!get_eilvt(offset)) {
  557. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  558. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  559. goto out;
  560. }
  561. valid = 1;
  562. out:
  563. preempt_enable();
  564. return valid;
  565. }
  566. static int setup_ibs_ctl(int ibs_eilvt_off)
  567. {
  568. struct pci_dev *cpu_cfg;
  569. int nodes;
  570. u32 value = 0;
  571. nodes = 0;
  572. cpu_cfg = NULL;
  573. do {
  574. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  575. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  576. cpu_cfg);
  577. if (!cpu_cfg)
  578. break;
  579. ++nodes;
  580. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  581. | IBSCTL_LVT_OFFSET_VALID);
  582. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  583. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  584. pci_dev_put(cpu_cfg);
  585. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  586. "IBSCTL = 0x%08x\n", value);
  587. return -EINVAL;
  588. }
  589. } while (1);
  590. if (!nodes) {
  591. printk(KERN_DEBUG "No CPU node configured for IBS\n");
  592. return -ENODEV;
  593. }
  594. return 0;
  595. }
  596. /*
  597. * This runs only on the current cpu. We try to find an LVT offset and
  598. * setup the local APIC. For this we must disable preemption. On
  599. * success we initialize all nodes with this offset. This updates then
  600. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  601. * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
  602. * is using the new offset.
  603. */
  604. static int force_ibs_eilvt_setup(void)
  605. {
  606. int offset;
  607. int ret;
  608. preempt_disable();
  609. /* find the next free available EILVT entry, skip offset 0 */
  610. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  611. if (get_eilvt(offset))
  612. break;
  613. }
  614. preempt_enable();
  615. if (offset == APIC_EILVT_NR_MAX) {
  616. printk(KERN_DEBUG "No EILVT entry available\n");
  617. return -EBUSY;
  618. }
  619. ret = setup_ibs_ctl(offset);
  620. if (ret)
  621. goto out;
  622. if (!ibs_eilvt_valid()) {
  623. ret = -EFAULT;
  624. goto out;
  625. }
  626. pr_info("IBS: LVT offset %d assigned\n", offset);
  627. return 0;
  628. out:
  629. preempt_disable();
  630. put_eilvt(offset);
  631. preempt_enable();
  632. return ret;
  633. }
  634. static inline int get_ibs_lvt_offset(void)
  635. {
  636. u64 val;
  637. rdmsrl(MSR_AMD64_IBSCTL, val);
  638. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  639. return -EINVAL;
  640. return val & IBSCTL_LVT_OFFSET_MASK;
  641. }
  642. static void setup_APIC_ibs(void *dummy)
  643. {
  644. int offset;
  645. offset = get_ibs_lvt_offset();
  646. if (offset < 0)
  647. goto failed;
  648. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  649. return;
  650. failed:
  651. pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
  652. smp_processor_id());
  653. }
  654. static void clear_APIC_ibs(void *dummy)
  655. {
  656. int offset;
  657. offset = get_ibs_lvt_offset();
  658. if (offset >= 0)
  659. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  660. }
  661. static int __cpuinit
  662. perf_ibs_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  663. {
  664. switch (action & ~CPU_TASKS_FROZEN) {
  665. case CPU_STARTING:
  666. setup_APIC_ibs(NULL);
  667. break;
  668. case CPU_DYING:
  669. clear_APIC_ibs(NULL);
  670. break;
  671. default:
  672. break;
  673. }
  674. return NOTIFY_OK;
  675. }
  676. static __init int amd_ibs_init(void)
  677. {
  678. u32 caps;
  679. int ret = -EINVAL;
  680. caps = __get_ibs_caps();
  681. if (!caps)
  682. return -ENODEV; /* ibs not supported by the cpu */
  683. /*
  684. * Force LVT offset assignment for family 10h: The offsets are
  685. * not assigned by the BIOS for this family, so the OS is
  686. * responsible for doing it. If the OS assignment fails, fall
  687. * back to BIOS settings and try to setup this.
  688. */
  689. if (boot_cpu_data.x86 == 0x10)
  690. force_ibs_eilvt_setup();
  691. if (!ibs_eilvt_valid())
  692. goto out;
  693. get_online_cpus();
  694. ibs_caps = caps;
  695. /* make ibs_caps visible to other cpus: */
  696. smp_mb();
  697. perf_cpu_notifier(perf_ibs_cpu_notifier);
  698. smp_call_function(setup_APIC_ibs, NULL, 1);
  699. put_online_cpus();
  700. ret = perf_event_ibs_init();
  701. out:
  702. if (ret)
  703. pr_err("Failed to setup IBS, %d\n", ret);
  704. return ret;
  705. }
  706. /* Since we need the pci subsystem to init ibs we can't do this earlier: */
  707. device_initcall(amd_ibs_init);