perf_event_amd_ibs.c 21 KB

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