ibs.c 24 KB

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