perf_event_amd_ibs.c 21 KB

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