perf_event_amd_ibs.c 19 KB

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