perf_event_amd_ibs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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);
  326. data.period = event->hw.last_period;
  327. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  328. ibs_data.caps = ibs_caps;
  329. size = 1;
  330. offset = 1;
  331. do {
  332. rdmsrl(msr + offset, *buf++);
  333. size++;
  334. offset = find_next_bit(perf_ibs->offset_mask,
  335. perf_ibs->offset_max,
  336. offset + 1);
  337. } while (offset < perf_ibs->offset_max);
  338. raw.size = sizeof(u32) + sizeof(u64) * size;
  339. raw.data = ibs_data.data;
  340. data.raw = &raw;
  341. }
  342. regs = *iregs; /* XXX: update ip from ibs sample */
  343. overflow = perf_ibs_set_period(perf_ibs, hwc, &config);
  344. reenable = !(overflow && perf_event_overflow(event, &data, &regs));
  345. config = (config >> 4) | (reenable ? perf_ibs->enable_mask : 0);
  346. perf_ibs_enable_event(hwc, config);
  347. perf_event_update_userpage(event);
  348. return 1;
  349. }
  350. static int __kprobes
  351. perf_ibs_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  352. {
  353. int handled = 0;
  354. handled += perf_ibs_handle_irq(&perf_ibs_fetch, regs);
  355. handled += perf_ibs_handle_irq(&perf_ibs_op, regs);
  356. if (handled)
  357. inc_irq_stat(apic_perf_irqs);
  358. return handled;
  359. }
  360. static __init int perf_ibs_pmu_init(struct perf_ibs *perf_ibs, char *name)
  361. {
  362. struct cpu_perf_ibs __percpu *pcpu;
  363. int ret;
  364. pcpu = alloc_percpu(struct cpu_perf_ibs);
  365. if (!pcpu)
  366. return -ENOMEM;
  367. perf_ibs->pcpu = pcpu;
  368. ret = perf_pmu_register(&perf_ibs->pmu, name, -1);
  369. if (ret) {
  370. perf_ibs->pcpu = NULL;
  371. free_percpu(pcpu);
  372. }
  373. return ret;
  374. }
  375. static __init int perf_event_ibs_init(void)
  376. {
  377. if (!ibs_caps)
  378. return -ENODEV; /* ibs not supported by the cpu */
  379. perf_ibs_pmu_init(&perf_ibs_fetch, "ibs_fetch");
  380. perf_ibs_pmu_init(&perf_ibs_op, "ibs_op");
  381. register_nmi_handler(NMI_LOCAL, perf_ibs_nmi_handler, 0, "perf_ibs");
  382. printk(KERN_INFO "perf: AMD IBS detected (0x%08x)\n", ibs_caps);
  383. return 0;
  384. }
  385. #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
  386. static __init int perf_event_ibs_init(void) { return 0; }
  387. #endif
  388. /* IBS - apic initialization, for perf and oprofile */
  389. static __init u32 __get_ibs_caps(void)
  390. {
  391. u32 caps;
  392. unsigned int max_level;
  393. if (!boot_cpu_has(X86_FEATURE_IBS))
  394. return 0;
  395. /* check IBS cpuid feature flags */
  396. max_level = cpuid_eax(0x80000000);
  397. if (max_level < IBS_CPUID_FEATURES)
  398. return IBS_CAPS_DEFAULT;
  399. caps = cpuid_eax(IBS_CPUID_FEATURES);
  400. if (!(caps & IBS_CAPS_AVAIL))
  401. /* cpuid flags not valid */
  402. return IBS_CAPS_DEFAULT;
  403. return caps;
  404. }
  405. u32 get_ibs_caps(void)
  406. {
  407. return ibs_caps;
  408. }
  409. EXPORT_SYMBOL(get_ibs_caps);
  410. static inline int get_eilvt(int offset)
  411. {
  412. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  413. }
  414. static inline int put_eilvt(int offset)
  415. {
  416. return !setup_APIC_eilvt(offset, 0, 0, 1);
  417. }
  418. /*
  419. * Check and reserve APIC extended interrupt LVT offset for IBS if available.
  420. */
  421. static inline int ibs_eilvt_valid(void)
  422. {
  423. int offset;
  424. u64 val;
  425. int valid = 0;
  426. preempt_disable();
  427. rdmsrl(MSR_AMD64_IBSCTL, val);
  428. offset = val & IBSCTL_LVT_OFFSET_MASK;
  429. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  430. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  431. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  432. goto out;
  433. }
  434. if (!get_eilvt(offset)) {
  435. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  436. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  437. goto out;
  438. }
  439. valid = 1;
  440. out:
  441. preempt_enable();
  442. return valid;
  443. }
  444. static int setup_ibs_ctl(int ibs_eilvt_off)
  445. {
  446. struct pci_dev *cpu_cfg;
  447. int nodes;
  448. u32 value = 0;
  449. nodes = 0;
  450. cpu_cfg = NULL;
  451. do {
  452. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  453. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  454. cpu_cfg);
  455. if (!cpu_cfg)
  456. break;
  457. ++nodes;
  458. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  459. | IBSCTL_LVT_OFFSET_VALID);
  460. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  461. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  462. pci_dev_put(cpu_cfg);
  463. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  464. "IBSCTL = 0x%08x\n", value);
  465. return -EINVAL;
  466. }
  467. } while (1);
  468. if (!nodes) {
  469. printk(KERN_DEBUG "No CPU node configured for IBS\n");
  470. return -ENODEV;
  471. }
  472. return 0;
  473. }
  474. /*
  475. * This runs only on the current cpu. We try to find an LVT offset and
  476. * setup the local APIC. For this we must disable preemption. On
  477. * success we initialize all nodes with this offset. This updates then
  478. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  479. * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
  480. * is using the new offset.
  481. */
  482. static int force_ibs_eilvt_setup(void)
  483. {
  484. int offset;
  485. int ret;
  486. preempt_disable();
  487. /* find the next free available EILVT entry, skip offset 0 */
  488. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  489. if (get_eilvt(offset))
  490. break;
  491. }
  492. preempt_enable();
  493. if (offset == APIC_EILVT_NR_MAX) {
  494. printk(KERN_DEBUG "No EILVT entry available\n");
  495. return -EBUSY;
  496. }
  497. ret = setup_ibs_ctl(offset);
  498. if (ret)
  499. goto out;
  500. if (!ibs_eilvt_valid()) {
  501. ret = -EFAULT;
  502. goto out;
  503. }
  504. pr_info("IBS: LVT offset %d assigned\n", offset);
  505. return 0;
  506. out:
  507. preempt_disable();
  508. put_eilvt(offset);
  509. preempt_enable();
  510. return ret;
  511. }
  512. static inline int get_ibs_lvt_offset(void)
  513. {
  514. u64 val;
  515. rdmsrl(MSR_AMD64_IBSCTL, val);
  516. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  517. return -EINVAL;
  518. return val & IBSCTL_LVT_OFFSET_MASK;
  519. }
  520. static void setup_APIC_ibs(void *dummy)
  521. {
  522. int offset;
  523. offset = get_ibs_lvt_offset();
  524. if (offset < 0)
  525. goto failed;
  526. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  527. return;
  528. failed:
  529. pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
  530. smp_processor_id());
  531. }
  532. static void clear_APIC_ibs(void *dummy)
  533. {
  534. int offset;
  535. offset = get_ibs_lvt_offset();
  536. if (offset >= 0)
  537. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  538. }
  539. static int __cpuinit
  540. perf_ibs_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  541. {
  542. switch (action & ~CPU_TASKS_FROZEN) {
  543. case CPU_STARTING:
  544. setup_APIC_ibs(NULL);
  545. break;
  546. case CPU_DYING:
  547. clear_APIC_ibs(NULL);
  548. break;
  549. default:
  550. break;
  551. }
  552. return NOTIFY_OK;
  553. }
  554. static __init int amd_ibs_init(void)
  555. {
  556. u32 caps;
  557. int ret = -EINVAL;
  558. caps = __get_ibs_caps();
  559. if (!caps)
  560. return -ENODEV; /* ibs not supported by the cpu */
  561. /*
  562. * Force LVT offset assignment for family 10h: The offsets are
  563. * not assigned by the BIOS for this family, so the OS is
  564. * responsible for doing it. If the OS assignment fails, fall
  565. * back to BIOS settings and try to setup this.
  566. */
  567. if (boot_cpu_data.x86 == 0x10)
  568. force_ibs_eilvt_setup();
  569. if (!ibs_eilvt_valid())
  570. goto out;
  571. get_online_cpus();
  572. ibs_caps = caps;
  573. /* make ibs_caps visible to other cpus: */
  574. smp_mb();
  575. perf_cpu_notifier(perf_ibs_cpu_notifier);
  576. smp_call_function(setup_APIC_ibs, NULL, 1);
  577. put_online_cpus();
  578. ret = perf_event_ibs_init();
  579. out:
  580. if (ret)
  581. pr_err("Failed to setup IBS, %d\n", ret);
  582. return ret;
  583. }
  584. /* Since we need the pci subsystem to init ibs we can't do this earlier: */
  585. device_initcall(amd_ibs_init);