perf_event_amd_ibs.c 20 KB

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