perf_event_amd_ibs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 <asm/apic.h>
  12. static u32 ibs_caps;
  13. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
  14. #include <linux/kprobes.h>
  15. #include <linux/hardirq.h>
  16. #include <asm/nmi.h>
  17. #define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)
  18. #define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT
  19. enum ibs_states {
  20. IBS_ENABLED = 0,
  21. IBS_STARTED = 1,
  22. IBS_STOPPING = 2,
  23. IBS_MAX_STATES,
  24. };
  25. struct cpu_perf_ibs {
  26. struct perf_event *event;
  27. unsigned long state[BITS_TO_LONGS(IBS_MAX_STATES)];
  28. };
  29. struct perf_ibs {
  30. struct pmu pmu;
  31. unsigned int msr;
  32. u64 config_mask;
  33. u64 cnt_mask;
  34. u64 enable_mask;
  35. u64 valid_mask;
  36. u64 max_period;
  37. unsigned long offset_mask[1];
  38. int offset_max;
  39. struct cpu_perf_ibs __percpu *pcpu;
  40. u64 (*get_count)(u64 config);
  41. };
  42. struct perf_ibs_data {
  43. u32 size;
  44. union {
  45. u32 data[0]; /* data buffer starts here */
  46. u32 caps;
  47. };
  48. u64 regs[MSR_AMD64_IBS_REG_COUNT_MAX];
  49. };
  50. static int
  51. perf_event_set_period(struct hw_perf_event *hwc, u64 min, u64 max, u64 *count)
  52. {
  53. s64 left = local64_read(&hwc->period_left);
  54. s64 period = hwc->sample_period;
  55. int overflow = 0;
  56. /*
  57. * If we are way outside a reasonable range then just skip forward:
  58. */
  59. if (unlikely(left <= -period)) {
  60. left = period;
  61. local64_set(&hwc->period_left, left);
  62. hwc->last_period = period;
  63. overflow = 1;
  64. }
  65. if (unlikely(left <= 0)) {
  66. left += period;
  67. local64_set(&hwc->period_left, left);
  68. hwc->last_period = period;
  69. overflow = 1;
  70. }
  71. if (unlikely(left < min))
  72. left = min;
  73. if (left > max)
  74. left = max;
  75. *count = (u64)left;
  76. return overflow;
  77. }
  78. static int
  79. perf_event_try_update(struct perf_event *event, u64 new_raw_count, int width)
  80. {
  81. struct hw_perf_event *hwc = &event->hw;
  82. int shift = 64 - width;
  83. u64 prev_raw_count;
  84. u64 delta;
  85. /*
  86. * Careful: an NMI might modify the previous event value.
  87. *
  88. * Our tactic to handle this is to first atomically read and
  89. * exchange a new raw count - then add that new-prev delta
  90. * count to the generic event atomically:
  91. */
  92. prev_raw_count = local64_read(&hwc->prev_count);
  93. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  94. new_raw_count) != prev_raw_count)
  95. return 0;
  96. /*
  97. * Now we have the new raw value and have updated the prev
  98. * timestamp already. We can now calculate the elapsed delta
  99. * (event-)time and add that to the generic event.
  100. *
  101. * Careful, not all hw sign-extends above the physical width
  102. * of the count.
  103. */
  104. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  105. delta >>= shift;
  106. local64_add(delta, &event->count);
  107. local64_sub(delta, &hwc->period_left);
  108. return 1;
  109. }
  110. static struct perf_ibs perf_ibs_fetch;
  111. static struct perf_ibs perf_ibs_op;
  112. static struct perf_ibs *get_ibs_pmu(int type)
  113. {
  114. if (perf_ibs_fetch.pmu.type == type)
  115. return &perf_ibs_fetch;
  116. if (perf_ibs_op.pmu.type == type)
  117. return &perf_ibs_op;
  118. return NULL;
  119. }
  120. static int perf_ibs_init(struct perf_event *event)
  121. {
  122. struct hw_perf_event *hwc = &event->hw;
  123. struct perf_ibs *perf_ibs;
  124. u64 max_cnt, config;
  125. perf_ibs = get_ibs_pmu(event->attr.type);
  126. if (!perf_ibs)
  127. return -ENOENT;
  128. config = event->attr.config;
  129. if (config & ~perf_ibs->config_mask)
  130. return -EINVAL;
  131. if (hwc->sample_period) {
  132. if (config & perf_ibs->cnt_mask)
  133. /* raw max_cnt may not be set */
  134. return -EINVAL;
  135. if (hwc->sample_period & 0x0f)
  136. /* lower 4 bits can not be set in ibs max cnt */
  137. return -EINVAL;
  138. } else {
  139. max_cnt = config & perf_ibs->cnt_mask;
  140. config &= ~perf_ibs->cnt_mask;
  141. event->attr.sample_period = max_cnt << 4;
  142. hwc->sample_period = event->attr.sample_period;
  143. }
  144. if (!hwc->sample_period)
  145. return -EINVAL;
  146. hwc->config_base = perf_ibs->msr;
  147. hwc->config = config;
  148. return 0;
  149. }
  150. static int perf_ibs_set_period(struct perf_ibs *perf_ibs,
  151. struct hw_perf_event *hwc, u64 *period)
  152. {
  153. int ret;
  154. /* ignore lower 4 bits in min count: */
  155. ret = perf_event_set_period(hwc, 1<<4, perf_ibs->max_period, period);
  156. local64_set(&hwc->prev_count, 0);
  157. return ret;
  158. }
  159. static u64 get_ibs_fetch_count(u64 config)
  160. {
  161. return (config & IBS_FETCH_CNT) >> 12;
  162. }
  163. static u64 get_ibs_op_count(u64 config)
  164. {
  165. return (config & IBS_OP_CUR_CNT) >> 32;
  166. }
  167. static void
  168. perf_ibs_event_update(struct perf_ibs *perf_ibs, struct perf_event *event,
  169. u64 config)
  170. {
  171. u64 count = perf_ibs->get_count(config);
  172. while (!perf_event_try_update(event, count, 20)) {
  173. rdmsrl(event->hw.config_base, config);
  174. count = perf_ibs->get_count(config);
  175. }
  176. }
  177. /* Note: The enable mask must be encoded in the config argument. */
  178. static inline void perf_ibs_enable_event(struct hw_perf_event *hwc, u64 config)
  179. {
  180. wrmsrl(hwc->config_base, hwc->config | config);
  181. }
  182. /*
  183. * We cannot restore the ibs pmu state, so we always needs to update
  184. * the event while stopping it and then reset the state when starting
  185. * again. Thus, ignoring PERF_EF_RELOAD and PERF_EF_UPDATE flags in
  186. * perf_ibs_start()/perf_ibs_stop() and instead always do it.
  187. */
  188. static void perf_ibs_start(struct perf_event *event, int flags)
  189. {
  190. struct hw_perf_event *hwc = &event->hw;
  191. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  192. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  193. u64 config;
  194. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  195. return;
  196. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  197. hwc->state = 0;
  198. perf_ibs_set_period(perf_ibs, hwc, &config);
  199. config = (config >> 4) | perf_ibs->enable_mask;
  200. set_bit(IBS_STARTED, pcpu->state);
  201. perf_ibs_enable_event(hwc, config);
  202. perf_event_update_userpage(event);
  203. }
  204. static void perf_ibs_stop(struct perf_event *event, int flags)
  205. {
  206. struct hw_perf_event *hwc = &event->hw;
  207. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  208. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  209. u64 val;
  210. int stopping;
  211. stopping = test_and_clear_bit(IBS_STARTED, pcpu->state);
  212. if (!stopping && (hwc->state & PERF_HES_UPTODATE))
  213. return;
  214. rdmsrl(hwc->config_base, val);
  215. if (stopping) {
  216. set_bit(IBS_STOPPING, pcpu->state);
  217. val &= ~perf_ibs->enable_mask;
  218. wrmsrl(hwc->config_base, val);
  219. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  220. hwc->state |= PERF_HES_STOPPED;
  221. }
  222. if (hwc->state & PERF_HES_UPTODATE)
  223. return;
  224. perf_ibs_event_update(perf_ibs, event, val);
  225. hwc->state |= PERF_HES_UPTODATE;
  226. }
  227. static int perf_ibs_add(struct perf_event *event, int flags)
  228. {
  229. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  230. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  231. if (test_and_set_bit(IBS_ENABLED, pcpu->state))
  232. return -ENOSPC;
  233. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  234. pcpu->event = event;
  235. if (flags & PERF_EF_START)
  236. perf_ibs_start(event, PERF_EF_RELOAD);
  237. return 0;
  238. }
  239. static void perf_ibs_del(struct perf_event *event, int flags)
  240. {
  241. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  242. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  243. if (!test_and_clear_bit(IBS_ENABLED, pcpu->state))
  244. return;
  245. perf_ibs_stop(event, PERF_EF_UPDATE);
  246. pcpu->event = NULL;
  247. perf_event_update_userpage(event);
  248. }
  249. static void perf_ibs_read(struct perf_event *event) { }
  250. static struct perf_ibs perf_ibs_fetch = {
  251. .pmu = {
  252. .task_ctx_nr = perf_invalid_context,
  253. .event_init = perf_ibs_init,
  254. .add = perf_ibs_add,
  255. .del = perf_ibs_del,
  256. .start = perf_ibs_start,
  257. .stop = perf_ibs_stop,
  258. .read = perf_ibs_read,
  259. },
  260. .msr = MSR_AMD64_IBSFETCHCTL,
  261. .config_mask = IBS_FETCH_CONFIG_MASK,
  262. .cnt_mask = IBS_FETCH_MAX_CNT,
  263. .enable_mask = IBS_FETCH_ENABLE,
  264. .valid_mask = IBS_FETCH_VAL,
  265. .max_period = IBS_FETCH_MAX_CNT << 4,
  266. .offset_mask = { MSR_AMD64_IBSFETCH_REG_MASK },
  267. .offset_max = MSR_AMD64_IBSFETCH_REG_COUNT,
  268. .get_count = get_ibs_fetch_count,
  269. };
  270. static struct perf_ibs perf_ibs_op = {
  271. .pmu = {
  272. .task_ctx_nr = perf_invalid_context,
  273. .event_init = perf_ibs_init,
  274. .add = perf_ibs_add,
  275. .del = perf_ibs_del,
  276. .start = perf_ibs_start,
  277. .stop = perf_ibs_stop,
  278. .read = perf_ibs_read,
  279. },
  280. .msr = MSR_AMD64_IBSOPCTL,
  281. .config_mask = IBS_OP_CONFIG_MASK,
  282. .cnt_mask = IBS_OP_MAX_CNT,
  283. .enable_mask = IBS_OP_ENABLE,
  284. .valid_mask = IBS_OP_VAL,
  285. .max_period = IBS_OP_MAX_CNT << 4,
  286. .offset_mask = { MSR_AMD64_IBSOP_REG_MASK },
  287. .offset_max = MSR_AMD64_IBSOP_REG_COUNT,
  288. .get_count = get_ibs_op_count,
  289. };
  290. static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
  291. {
  292. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  293. struct perf_event *event = pcpu->event;
  294. struct hw_perf_event *hwc = &event->hw;
  295. struct perf_sample_data data;
  296. struct perf_raw_record raw;
  297. struct pt_regs regs;
  298. struct perf_ibs_data ibs_data;
  299. int offset, size, overflow, reenable;
  300. unsigned int msr;
  301. u64 *buf, config;
  302. if (!test_bit(IBS_STARTED, pcpu->state)) {
  303. /* Catch spurious interrupts after stopping IBS: */
  304. if (!test_and_clear_bit(IBS_STOPPING, pcpu->state))
  305. return 0;
  306. rdmsrl(perf_ibs->msr, *ibs_data.regs);
  307. return (*ibs_data.regs & perf_ibs->valid_mask) ? 1 : 0;
  308. }
  309. msr = hwc->config_base;
  310. buf = ibs_data.regs;
  311. rdmsrl(msr, *buf);
  312. if (!(*buf++ & perf_ibs->valid_mask))
  313. return 0;
  314. /*
  315. * Emulate IbsOpCurCnt in MSRC001_1033 (IbsOpCtl), not
  316. * supported in all cpus. As this triggered an interrupt, we
  317. * set the current count to the max count.
  318. */
  319. config = ibs_data.regs[0];
  320. if (perf_ibs == &perf_ibs_op && !(ibs_caps & IBS_CAPS_RDWROPCNT)) {
  321. config &= ~IBS_OP_CUR_CNT;
  322. config |= (config & IBS_OP_MAX_CNT) << 36;
  323. }
  324. perf_ibs_event_update(perf_ibs, event, config);
  325. perf_sample_data_init(&data, 0, hwc->last_period);
  326. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  327. ibs_data.caps = ibs_caps;
  328. size = 1;
  329. offset = 1;
  330. do {
  331. rdmsrl(msr + offset, *buf++);
  332. size++;
  333. offset = find_next_bit(perf_ibs->offset_mask,
  334. perf_ibs->offset_max,
  335. offset + 1);
  336. } while (offset < perf_ibs->offset_max);
  337. raw.size = sizeof(u32) + sizeof(u64) * size;
  338. raw.data = ibs_data.data;
  339. data.raw = &raw;
  340. }
  341. regs = *iregs; /* XXX: update ip from ibs sample */
  342. overflow = perf_ibs_set_period(perf_ibs, hwc, &config);
  343. reenable = !(overflow && perf_event_overflow(event, &data, &regs));
  344. config = (config >> 4) | (reenable ? perf_ibs->enable_mask : 0);
  345. perf_ibs_enable_event(hwc, config);
  346. perf_event_update_userpage(event);
  347. return 1;
  348. }
  349. static int __kprobes
  350. perf_ibs_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  351. {
  352. int handled = 0;
  353. handled += perf_ibs_handle_irq(&perf_ibs_fetch, regs);
  354. handled += perf_ibs_handle_irq(&perf_ibs_op, regs);
  355. if (handled)
  356. inc_irq_stat(apic_perf_irqs);
  357. return handled;
  358. }
  359. static __init int perf_ibs_pmu_init(struct perf_ibs *perf_ibs, char *name)
  360. {
  361. struct cpu_perf_ibs __percpu *pcpu;
  362. int ret;
  363. pcpu = alloc_percpu(struct cpu_perf_ibs);
  364. if (!pcpu)
  365. return -ENOMEM;
  366. perf_ibs->pcpu = pcpu;
  367. ret = perf_pmu_register(&perf_ibs->pmu, name, -1);
  368. if (ret) {
  369. perf_ibs->pcpu = NULL;
  370. free_percpu(pcpu);
  371. }
  372. return ret;
  373. }
  374. static __init int perf_event_ibs_init(void)
  375. {
  376. if (!ibs_caps)
  377. return -ENODEV; /* ibs not supported by the cpu */
  378. perf_ibs_pmu_init(&perf_ibs_fetch, "ibs_fetch");
  379. if (ibs_caps & IBS_CAPS_OPCNT)
  380. perf_ibs_op.config_mask |= IBS_OP_CNT_CTL;
  381. perf_ibs_pmu_init(&perf_ibs_op, "ibs_op");
  382. register_nmi_handler(NMI_LOCAL, perf_ibs_nmi_handler, 0, "perf_ibs");
  383. printk(KERN_INFO "perf: AMD IBS detected (0x%08x)\n", ibs_caps);
  384. return 0;
  385. }
  386. #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
  387. static __init int perf_event_ibs_init(void) { return 0; }
  388. #endif
  389. /* IBS - apic initialization, for perf and oprofile */
  390. static __init u32 __get_ibs_caps(void)
  391. {
  392. u32 caps;
  393. unsigned int max_level;
  394. if (!boot_cpu_has(X86_FEATURE_IBS))
  395. return 0;
  396. /* check IBS cpuid feature flags */
  397. max_level = cpuid_eax(0x80000000);
  398. if (max_level < IBS_CPUID_FEATURES)
  399. return IBS_CAPS_DEFAULT;
  400. caps = cpuid_eax(IBS_CPUID_FEATURES);
  401. if (!(caps & IBS_CAPS_AVAIL))
  402. /* cpuid flags not valid */
  403. return IBS_CAPS_DEFAULT;
  404. return caps;
  405. }
  406. u32 get_ibs_caps(void)
  407. {
  408. return ibs_caps;
  409. }
  410. EXPORT_SYMBOL(get_ibs_caps);
  411. static inline int get_eilvt(int offset)
  412. {
  413. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  414. }
  415. static inline int put_eilvt(int offset)
  416. {
  417. return !setup_APIC_eilvt(offset, 0, 0, 1);
  418. }
  419. /*
  420. * Check and reserve APIC extended interrupt LVT offset for IBS if available.
  421. */
  422. static inline int ibs_eilvt_valid(void)
  423. {
  424. int offset;
  425. u64 val;
  426. int valid = 0;
  427. preempt_disable();
  428. rdmsrl(MSR_AMD64_IBSCTL, val);
  429. offset = val & IBSCTL_LVT_OFFSET_MASK;
  430. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  431. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  432. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  433. goto out;
  434. }
  435. if (!get_eilvt(offset)) {
  436. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  437. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  438. goto out;
  439. }
  440. valid = 1;
  441. out:
  442. preempt_enable();
  443. return valid;
  444. }
  445. static int setup_ibs_ctl(int ibs_eilvt_off)
  446. {
  447. struct pci_dev *cpu_cfg;
  448. int nodes;
  449. u32 value = 0;
  450. nodes = 0;
  451. cpu_cfg = NULL;
  452. do {
  453. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  454. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  455. cpu_cfg);
  456. if (!cpu_cfg)
  457. break;
  458. ++nodes;
  459. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  460. | IBSCTL_LVT_OFFSET_VALID);
  461. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  462. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  463. pci_dev_put(cpu_cfg);
  464. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  465. "IBSCTL = 0x%08x\n", value);
  466. return -EINVAL;
  467. }
  468. } while (1);
  469. if (!nodes) {
  470. printk(KERN_DEBUG "No CPU node configured for IBS\n");
  471. return -ENODEV;
  472. }
  473. return 0;
  474. }
  475. /*
  476. * This runs only on the current cpu. We try to find an LVT offset and
  477. * setup the local APIC. For this we must disable preemption. On
  478. * success we initialize all nodes with this offset. This updates then
  479. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  480. * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
  481. * is using the new offset.
  482. */
  483. static int force_ibs_eilvt_setup(void)
  484. {
  485. int offset;
  486. int ret;
  487. preempt_disable();
  488. /* find the next free available EILVT entry, skip offset 0 */
  489. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  490. if (get_eilvt(offset))
  491. break;
  492. }
  493. preempt_enable();
  494. if (offset == APIC_EILVT_NR_MAX) {
  495. printk(KERN_DEBUG "No EILVT entry available\n");
  496. return -EBUSY;
  497. }
  498. ret = setup_ibs_ctl(offset);
  499. if (ret)
  500. goto out;
  501. if (!ibs_eilvt_valid()) {
  502. ret = -EFAULT;
  503. goto out;
  504. }
  505. pr_info("IBS: LVT offset %d assigned\n", offset);
  506. return 0;
  507. out:
  508. preempt_disable();
  509. put_eilvt(offset);
  510. preempt_enable();
  511. return ret;
  512. }
  513. static inline int get_ibs_lvt_offset(void)
  514. {
  515. u64 val;
  516. rdmsrl(MSR_AMD64_IBSCTL, val);
  517. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  518. return -EINVAL;
  519. return val & IBSCTL_LVT_OFFSET_MASK;
  520. }
  521. static void setup_APIC_ibs(void *dummy)
  522. {
  523. int offset;
  524. offset = get_ibs_lvt_offset();
  525. if (offset < 0)
  526. goto failed;
  527. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  528. return;
  529. failed:
  530. pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
  531. smp_processor_id());
  532. }
  533. static void clear_APIC_ibs(void *dummy)
  534. {
  535. int offset;
  536. offset = get_ibs_lvt_offset();
  537. if (offset >= 0)
  538. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  539. }
  540. static int __cpuinit
  541. perf_ibs_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  542. {
  543. switch (action & ~CPU_TASKS_FROZEN) {
  544. case CPU_STARTING:
  545. setup_APIC_ibs(NULL);
  546. break;
  547. case CPU_DYING:
  548. clear_APIC_ibs(NULL);
  549. break;
  550. default:
  551. break;
  552. }
  553. return NOTIFY_OK;
  554. }
  555. static __init int amd_ibs_init(void)
  556. {
  557. u32 caps;
  558. int ret = -EINVAL;
  559. caps = __get_ibs_caps();
  560. if (!caps)
  561. return -ENODEV; /* ibs not supported by the cpu */
  562. /*
  563. * Force LVT offset assignment for family 10h: The offsets are
  564. * not assigned by the BIOS for this family, so the OS is
  565. * responsible for doing it. If the OS assignment fails, fall
  566. * back to BIOS settings and try to setup this.
  567. */
  568. if (boot_cpu_data.x86 == 0x10)
  569. force_ibs_eilvt_setup();
  570. if (!ibs_eilvt_valid())
  571. goto out;
  572. get_online_cpus();
  573. ibs_caps = caps;
  574. /* make ibs_caps visible to other cpus: */
  575. smp_mb();
  576. perf_cpu_notifier(perf_ibs_cpu_notifier);
  577. smp_call_function(setup_APIC_ibs, NULL, 1);
  578. put_online_cpus();
  579. ret = perf_event_ibs_init();
  580. out:
  581. if (ret)
  582. pr_err("Failed to setup IBS, %d\n", ret);
  583. return ret;
  584. }
  585. /* Since we need the pci subsystem to init ibs we can't do this earlier: */
  586. device_initcall(amd_ibs_init);