perf_event_amd_ibs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 int perf_ibs_init(struct perf_event *event)
  183. {
  184. struct hw_perf_event *hwc = &event->hw;
  185. struct perf_ibs *perf_ibs;
  186. u64 max_cnt, config;
  187. int ret;
  188. perf_ibs = get_ibs_pmu(event->attr.type);
  189. if (perf_ibs) {
  190. config = event->attr.config;
  191. } else {
  192. perf_ibs = &perf_ibs_op;
  193. ret = perf_ibs_precise_event(event, &config);
  194. if (ret)
  195. return ret;
  196. }
  197. if (event->pmu != &perf_ibs->pmu)
  198. return -ENOENT;
  199. if (config & ~perf_ibs->config_mask)
  200. return -EINVAL;
  201. if (hwc->sample_period) {
  202. if (config & perf_ibs->cnt_mask)
  203. /* raw max_cnt may not be set */
  204. return -EINVAL;
  205. if (!event->attr.sample_freq && hwc->sample_period & 0x0f)
  206. /*
  207. * lower 4 bits can not be set in ibs max cnt,
  208. * but allowing it in case we adjust the
  209. * sample period to set a frequency.
  210. */
  211. return -EINVAL;
  212. hwc->sample_period &= ~0x0FULL;
  213. if (!hwc->sample_period)
  214. hwc->sample_period = 0x10;
  215. } else {
  216. max_cnt = config & perf_ibs->cnt_mask;
  217. config &= ~perf_ibs->cnt_mask;
  218. event->attr.sample_period = max_cnt << 4;
  219. hwc->sample_period = event->attr.sample_period;
  220. }
  221. if (!hwc->sample_period)
  222. return -EINVAL;
  223. /*
  224. * If we modify hwc->sample_period, we also need to update
  225. * hwc->last_period and hwc->period_left.
  226. */
  227. hwc->last_period = hwc->sample_period;
  228. local64_set(&hwc->period_left, hwc->sample_period);
  229. hwc->config_base = perf_ibs->msr;
  230. hwc->config = config;
  231. return 0;
  232. }
  233. static int perf_ibs_set_period(struct perf_ibs *perf_ibs,
  234. struct hw_perf_event *hwc, u64 *period)
  235. {
  236. int overflow;
  237. /* ignore lower 4 bits in min count: */
  238. overflow = perf_event_set_period(hwc, 1<<4, perf_ibs->max_period, period);
  239. local64_set(&hwc->prev_count, 0);
  240. return overflow;
  241. }
  242. static u64 get_ibs_fetch_count(u64 config)
  243. {
  244. return (config & IBS_FETCH_CNT) >> 12;
  245. }
  246. static u64 get_ibs_op_count(u64 config)
  247. {
  248. u64 count = 0;
  249. if (config & IBS_OP_VAL)
  250. count += (config & IBS_OP_MAX_CNT) << 4; /* cnt rolled over */
  251. if (ibs_caps & IBS_CAPS_RDWROPCNT)
  252. count += (config & IBS_OP_CUR_CNT) >> 32;
  253. return count;
  254. }
  255. static void
  256. perf_ibs_event_update(struct perf_ibs *perf_ibs, struct perf_event *event,
  257. u64 *config)
  258. {
  259. u64 count = perf_ibs->get_count(*config);
  260. /*
  261. * Set width to 64 since we do not overflow on max width but
  262. * instead on max count. In perf_ibs_set_period() we clear
  263. * prev count manually on overflow.
  264. */
  265. while (!perf_event_try_update(event, count, 64)) {
  266. rdmsrl(event->hw.config_base, *config);
  267. count = perf_ibs->get_count(*config);
  268. }
  269. }
  270. static inline void perf_ibs_enable_event(struct perf_ibs *perf_ibs,
  271. struct hw_perf_event *hwc, u64 config)
  272. {
  273. wrmsrl(hwc->config_base, hwc->config | config | perf_ibs->enable_mask);
  274. }
  275. /*
  276. * Erratum #420 Instruction-Based Sampling Engine May Generate
  277. * Interrupt that Cannot Be Cleared:
  278. *
  279. * Must clear counter mask first, then clear the enable bit. See
  280. * Revision Guide for AMD Family 10h Processors, Publication #41322.
  281. */
  282. static inline void perf_ibs_disable_event(struct perf_ibs *perf_ibs,
  283. struct hw_perf_event *hwc, u64 config)
  284. {
  285. config &= ~perf_ibs->cnt_mask;
  286. wrmsrl(hwc->config_base, config);
  287. config &= ~perf_ibs->enable_mask;
  288. wrmsrl(hwc->config_base, config);
  289. }
  290. /*
  291. * We cannot restore the ibs pmu state, so we always needs to update
  292. * the event while stopping it and then reset the state when starting
  293. * again. Thus, ignoring PERF_EF_RELOAD and PERF_EF_UPDATE flags in
  294. * perf_ibs_start()/perf_ibs_stop() and instead always do it.
  295. */
  296. static void perf_ibs_start(struct perf_event *event, int flags)
  297. {
  298. struct hw_perf_event *hwc = &event->hw;
  299. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  300. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  301. u64 period;
  302. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  303. return;
  304. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  305. hwc->state = 0;
  306. perf_ibs_set_period(perf_ibs, hwc, &period);
  307. set_bit(IBS_STARTED, pcpu->state);
  308. perf_ibs_enable_event(perf_ibs, hwc, period >> 4);
  309. perf_event_update_userpage(event);
  310. }
  311. static void perf_ibs_stop(struct perf_event *event, int flags)
  312. {
  313. struct hw_perf_event *hwc = &event->hw;
  314. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  315. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  316. u64 config;
  317. int stopping;
  318. stopping = test_and_clear_bit(IBS_STARTED, pcpu->state);
  319. if (!stopping && (hwc->state & PERF_HES_UPTODATE))
  320. return;
  321. rdmsrl(hwc->config_base, config);
  322. if (stopping) {
  323. set_bit(IBS_STOPPING, pcpu->state);
  324. perf_ibs_disable_event(perf_ibs, hwc, config);
  325. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  326. hwc->state |= PERF_HES_STOPPED;
  327. }
  328. if (hwc->state & PERF_HES_UPTODATE)
  329. return;
  330. /*
  331. * Clear valid bit to not count rollovers on update, rollovers
  332. * are only updated in the irq handler.
  333. */
  334. config &= ~perf_ibs->valid_mask;
  335. perf_ibs_event_update(perf_ibs, event, &config);
  336. hwc->state |= PERF_HES_UPTODATE;
  337. }
  338. static int perf_ibs_add(struct perf_event *event, int flags)
  339. {
  340. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  341. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  342. if (test_and_set_bit(IBS_ENABLED, pcpu->state))
  343. return -ENOSPC;
  344. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  345. pcpu->event = event;
  346. if (flags & PERF_EF_START)
  347. perf_ibs_start(event, PERF_EF_RELOAD);
  348. return 0;
  349. }
  350. static void perf_ibs_del(struct perf_event *event, int flags)
  351. {
  352. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  353. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  354. if (!test_and_clear_bit(IBS_ENABLED, pcpu->state))
  355. return;
  356. perf_ibs_stop(event, PERF_EF_UPDATE);
  357. pcpu->event = NULL;
  358. perf_event_update_userpage(event);
  359. }
  360. static void perf_ibs_read(struct perf_event *event) { }
  361. static struct perf_ibs perf_ibs_fetch = {
  362. .pmu = {
  363. .task_ctx_nr = perf_invalid_context,
  364. .event_init = perf_ibs_init,
  365. .add = perf_ibs_add,
  366. .del = perf_ibs_del,
  367. .start = perf_ibs_start,
  368. .stop = perf_ibs_stop,
  369. .read = perf_ibs_read,
  370. },
  371. .msr = MSR_AMD64_IBSFETCHCTL,
  372. .config_mask = IBS_FETCH_CONFIG_MASK,
  373. .cnt_mask = IBS_FETCH_MAX_CNT,
  374. .enable_mask = IBS_FETCH_ENABLE,
  375. .valid_mask = IBS_FETCH_VAL,
  376. .max_period = IBS_FETCH_MAX_CNT << 4,
  377. .offset_mask = { MSR_AMD64_IBSFETCH_REG_MASK },
  378. .offset_max = MSR_AMD64_IBSFETCH_REG_COUNT,
  379. .get_count = get_ibs_fetch_count,
  380. };
  381. static struct perf_ibs perf_ibs_op = {
  382. .pmu = {
  383. .task_ctx_nr = perf_invalid_context,
  384. .event_init = perf_ibs_init,
  385. .add = perf_ibs_add,
  386. .del = perf_ibs_del,
  387. .start = perf_ibs_start,
  388. .stop = perf_ibs_stop,
  389. .read = perf_ibs_read,
  390. },
  391. .msr = MSR_AMD64_IBSOPCTL,
  392. .config_mask = IBS_OP_CONFIG_MASK,
  393. .cnt_mask = IBS_OP_MAX_CNT,
  394. .enable_mask = IBS_OP_ENABLE,
  395. .valid_mask = IBS_OP_VAL,
  396. .max_period = IBS_OP_MAX_CNT << 4,
  397. .offset_mask = { MSR_AMD64_IBSOP_REG_MASK },
  398. .offset_max = MSR_AMD64_IBSOP_REG_COUNT,
  399. .get_count = get_ibs_op_count,
  400. };
  401. static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
  402. {
  403. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  404. struct perf_event *event = pcpu->event;
  405. struct hw_perf_event *hwc = &event->hw;
  406. struct perf_sample_data data;
  407. struct perf_raw_record raw;
  408. struct pt_regs regs;
  409. struct perf_ibs_data ibs_data;
  410. int offset, size, check_rip, offset_max, throttle = 0;
  411. unsigned int msr;
  412. u64 *buf, *config, period;
  413. if (!test_bit(IBS_STARTED, pcpu->state)) {
  414. /*
  415. * Catch spurious interrupts after stopping IBS: After
  416. * disabling IBS there could be still incomming NMIs
  417. * with samples that even have the valid bit cleared.
  418. * Mark all this NMIs as handled.
  419. */
  420. return test_and_clear_bit(IBS_STOPPING, pcpu->state) ? 1 : 0;
  421. }
  422. msr = hwc->config_base;
  423. buf = ibs_data.regs;
  424. rdmsrl(msr, *buf);
  425. if (!(*buf++ & perf_ibs->valid_mask))
  426. return 0;
  427. config = &ibs_data.regs[0];
  428. perf_ibs_event_update(perf_ibs, event, config);
  429. perf_sample_data_init(&data, 0, hwc->last_period);
  430. if (!perf_ibs_set_period(perf_ibs, hwc, &period))
  431. goto out; /* no sw counter overflow */
  432. ibs_data.caps = ibs_caps;
  433. size = 1;
  434. offset = 1;
  435. check_rip = (perf_ibs == &perf_ibs_op && (ibs_caps & IBS_CAPS_RIPINVALIDCHK));
  436. if (event->attr.sample_type & PERF_SAMPLE_RAW)
  437. offset_max = perf_ibs->offset_max;
  438. else if (check_rip)
  439. offset_max = 2;
  440. else
  441. offset_max = 1;
  442. do {
  443. rdmsrl(msr + offset, *buf++);
  444. size++;
  445. offset = find_next_bit(perf_ibs->offset_mask,
  446. perf_ibs->offset_max,
  447. offset + 1);
  448. } while (offset < offset_max);
  449. ibs_data.size = sizeof(u64) * size;
  450. regs = *iregs;
  451. if (check_rip && (ibs_data.regs[2] & IBS_RIP_INVALID)) {
  452. regs.flags &= ~PERF_EFLAGS_EXACT;
  453. } else {
  454. set_linear_ip(&regs, ibs_data.regs[1]);
  455. regs.flags |= PERF_EFLAGS_EXACT;
  456. }
  457. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  458. raw.size = sizeof(u32) + ibs_data.size;
  459. raw.data = ibs_data.data;
  460. data.raw = &raw;
  461. }
  462. throttle = perf_event_overflow(event, &data, &regs);
  463. out:
  464. if (throttle)
  465. perf_ibs_disable_event(perf_ibs, hwc, *config);
  466. else
  467. perf_ibs_enable_event(perf_ibs, hwc, period >> 4);
  468. perf_event_update_userpage(event);
  469. return 1;
  470. }
  471. static int __kprobes
  472. perf_ibs_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  473. {
  474. int handled = 0;
  475. handled += perf_ibs_handle_irq(&perf_ibs_fetch, regs);
  476. handled += perf_ibs_handle_irq(&perf_ibs_op, regs);
  477. if (handled)
  478. inc_irq_stat(apic_perf_irqs);
  479. return handled;
  480. }
  481. static __init int perf_ibs_pmu_init(struct perf_ibs *perf_ibs, char *name)
  482. {
  483. struct cpu_perf_ibs __percpu *pcpu;
  484. int ret;
  485. pcpu = alloc_percpu(struct cpu_perf_ibs);
  486. if (!pcpu)
  487. return -ENOMEM;
  488. perf_ibs->pcpu = pcpu;
  489. ret = perf_pmu_register(&perf_ibs->pmu, name, -1);
  490. if (ret) {
  491. perf_ibs->pcpu = NULL;
  492. free_percpu(pcpu);
  493. }
  494. return ret;
  495. }
  496. static __init int perf_event_ibs_init(void)
  497. {
  498. if (!ibs_caps)
  499. return -ENODEV; /* ibs not supported by the cpu */
  500. perf_ibs_pmu_init(&perf_ibs_fetch, "ibs_fetch");
  501. if (ibs_caps & IBS_CAPS_OPCNT)
  502. perf_ibs_op.config_mask |= IBS_OP_CNT_CTL;
  503. perf_ibs_pmu_init(&perf_ibs_op, "ibs_op");
  504. register_nmi_handler(NMI_LOCAL, perf_ibs_nmi_handler, 0, "perf_ibs");
  505. printk(KERN_INFO "perf: AMD IBS detected (0x%08x)\n", ibs_caps);
  506. return 0;
  507. }
  508. #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
  509. static __init int perf_event_ibs_init(void) { return 0; }
  510. #endif
  511. /* IBS - apic initialization, for perf and oprofile */
  512. static __init u32 __get_ibs_caps(void)
  513. {
  514. u32 caps;
  515. unsigned int max_level;
  516. if (!boot_cpu_has(X86_FEATURE_IBS))
  517. return 0;
  518. /* check IBS cpuid feature flags */
  519. max_level = cpuid_eax(0x80000000);
  520. if (max_level < IBS_CPUID_FEATURES)
  521. return IBS_CAPS_DEFAULT;
  522. caps = cpuid_eax(IBS_CPUID_FEATURES);
  523. if (!(caps & IBS_CAPS_AVAIL))
  524. /* cpuid flags not valid */
  525. return IBS_CAPS_DEFAULT;
  526. return caps;
  527. }
  528. u32 get_ibs_caps(void)
  529. {
  530. return ibs_caps;
  531. }
  532. EXPORT_SYMBOL(get_ibs_caps);
  533. static inline int get_eilvt(int offset)
  534. {
  535. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  536. }
  537. static inline int put_eilvt(int offset)
  538. {
  539. return !setup_APIC_eilvt(offset, 0, 0, 1);
  540. }
  541. /*
  542. * Check and reserve APIC extended interrupt LVT offset for IBS if available.
  543. */
  544. static inline int ibs_eilvt_valid(void)
  545. {
  546. int offset;
  547. u64 val;
  548. int valid = 0;
  549. preempt_disable();
  550. rdmsrl(MSR_AMD64_IBSCTL, val);
  551. offset = val & IBSCTL_LVT_OFFSET_MASK;
  552. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  553. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  554. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  555. goto out;
  556. }
  557. if (!get_eilvt(offset)) {
  558. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  559. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  560. goto out;
  561. }
  562. valid = 1;
  563. out:
  564. preempt_enable();
  565. return valid;
  566. }
  567. static int setup_ibs_ctl(int ibs_eilvt_off)
  568. {
  569. struct pci_dev *cpu_cfg;
  570. int nodes;
  571. u32 value = 0;
  572. nodes = 0;
  573. cpu_cfg = NULL;
  574. do {
  575. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  576. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  577. cpu_cfg);
  578. if (!cpu_cfg)
  579. break;
  580. ++nodes;
  581. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  582. | IBSCTL_LVT_OFFSET_VALID);
  583. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  584. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  585. pci_dev_put(cpu_cfg);
  586. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  587. "IBSCTL = 0x%08x\n", value);
  588. return -EINVAL;
  589. }
  590. } while (1);
  591. if (!nodes) {
  592. printk(KERN_DEBUG "No CPU node configured for IBS\n");
  593. return -ENODEV;
  594. }
  595. return 0;
  596. }
  597. /*
  598. * This runs only on the current cpu. We try to find an LVT offset and
  599. * setup the local APIC. For this we must disable preemption. On
  600. * success we initialize all nodes with this offset. This updates then
  601. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  602. * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
  603. * is using the new offset.
  604. */
  605. static int force_ibs_eilvt_setup(void)
  606. {
  607. int offset;
  608. int ret;
  609. preempt_disable();
  610. /* find the next free available EILVT entry, skip offset 0 */
  611. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  612. if (get_eilvt(offset))
  613. break;
  614. }
  615. preempt_enable();
  616. if (offset == APIC_EILVT_NR_MAX) {
  617. printk(KERN_DEBUG "No EILVT entry available\n");
  618. return -EBUSY;
  619. }
  620. ret = setup_ibs_ctl(offset);
  621. if (ret)
  622. goto out;
  623. if (!ibs_eilvt_valid()) {
  624. ret = -EFAULT;
  625. goto out;
  626. }
  627. pr_info("IBS: LVT offset %d assigned\n", offset);
  628. return 0;
  629. out:
  630. preempt_disable();
  631. put_eilvt(offset);
  632. preempt_enable();
  633. return ret;
  634. }
  635. static inline int get_ibs_lvt_offset(void)
  636. {
  637. u64 val;
  638. rdmsrl(MSR_AMD64_IBSCTL, val);
  639. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  640. return -EINVAL;
  641. return val & IBSCTL_LVT_OFFSET_MASK;
  642. }
  643. static void setup_APIC_ibs(void *dummy)
  644. {
  645. int offset;
  646. offset = get_ibs_lvt_offset();
  647. if (offset < 0)
  648. goto failed;
  649. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  650. return;
  651. failed:
  652. pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
  653. smp_processor_id());
  654. }
  655. static void clear_APIC_ibs(void *dummy)
  656. {
  657. int offset;
  658. offset = get_ibs_lvt_offset();
  659. if (offset >= 0)
  660. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  661. }
  662. static int __cpuinit
  663. perf_ibs_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  664. {
  665. switch (action & ~CPU_TASKS_FROZEN) {
  666. case CPU_STARTING:
  667. setup_APIC_ibs(NULL);
  668. break;
  669. case CPU_DYING:
  670. clear_APIC_ibs(NULL);
  671. break;
  672. default:
  673. break;
  674. }
  675. return NOTIFY_OK;
  676. }
  677. static __init int amd_ibs_init(void)
  678. {
  679. u32 caps;
  680. int ret = -EINVAL;
  681. caps = __get_ibs_caps();
  682. if (!caps)
  683. return -ENODEV; /* ibs not supported by the cpu */
  684. /*
  685. * Force LVT offset assignment for family 10h: The offsets are
  686. * not assigned by the BIOS for this family, so the OS is
  687. * responsible for doing it. If the OS assignment fails, fall
  688. * back to BIOS settings and try to setup this.
  689. */
  690. if (boot_cpu_data.x86 == 0x10)
  691. force_ibs_eilvt_setup();
  692. if (!ibs_eilvt_valid())
  693. goto out;
  694. get_online_cpus();
  695. ibs_caps = caps;
  696. /* make ibs_caps visible to other cpus: */
  697. smp_mb();
  698. perf_cpu_notifier(perf_ibs_cpu_notifier);
  699. smp_call_function(setup_APIC_ibs, NULL, 1);
  700. put_online_cpus();
  701. ret = perf_event_ibs_init();
  702. out:
  703. if (ret)
  704. pr_err("Failed to setup IBS, %d\n", ret);
  705. return ret;
  706. }
  707. /* Since we need the pci subsystem to init ibs we can't do this earlier: */
  708. device_initcall(amd_ibs_init);