perf_event_amd_ibs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  476. /*
  477. * Read IbsBrTarget and IbsOpData4 separately
  478. * depending on their availability.
  479. * Can't add to offset_max as they are staggered
  480. */
  481. if (ibs_caps & IBS_CAPS_BRNTRGT) {
  482. rdmsrl(MSR_AMD64_IBSBRTARGET, *buf++);
  483. size++;
  484. }
  485. if (ibs_caps & IBS_CAPS_OPDATA4) {
  486. rdmsrl(MSR_AMD64_IBSOPDATA4, *buf++);
  487. size++;
  488. }
  489. }
  490. ibs_data.size = sizeof(u64) * size;
  491. regs = *iregs;
  492. if (check_rip && (ibs_data.regs[2] & IBS_RIP_INVALID)) {
  493. regs.flags &= ~PERF_EFLAGS_EXACT;
  494. } else {
  495. set_linear_ip(&regs, ibs_data.regs[1]);
  496. regs.flags |= PERF_EFLAGS_EXACT;
  497. }
  498. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  499. raw.size = sizeof(u32) + ibs_data.size;
  500. raw.data = ibs_data.data;
  501. data.raw = &raw;
  502. }
  503. throttle = perf_event_overflow(event, &data, &regs);
  504. out:
  505. if (throttle)
  506. perf_ibs_disable_event(perf_ibs, hwc, *config);
  507. else
  508. perf_ibs_enable_event(perf_ibs, hwc, period >> 4);
  509. perf_event_update_userpage(event);
  510. return 1;
  511. }
  512. static int
  513. perf_ibs_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  514. {
  515. int handled = 0;
  516. handled += perf_ibs_handle_irq(&perf_ibs_fetch, regs);
  517. handled += perf_ibs_handle_irq(&perf_ibs_op, regs);
  518. if (handled)
  519. inc_irq_stat(apic_perf_irqs);
  520. return handled;
  521. }
  522. NOKPROBE_SYMBOL(perf_ibs_nmi_handler);
  523. static __init int perf_ibs_pmu_init(struct perf_ibs *perf_ibs, char *name)
  524. {
  525. struct cpu_perf_ibs __percpu *pcpu;
  526. int ret;
  527. pcpu = alloc_percpu(struct cpu_perf_ibs);
  528. if (!pcpu)
  529. return -ENOMEM;
  530. perf_ibs->pcpu = pcpu;
  531. /* register attributes */
  532. if (perf_ibs->format_attrs[0]) {
  533. memset(&perf_ibs->format_group, 0, sizeof(perf_ibs->format_group));
  534. perf_ibs->format_group.name = "format";
  535. perf_ibs->format_group.attrs = perf_ibs->format_attrs;
  536. memset(&perf_ibs->attr_groups, 0, sizeof(perf_ibs->attr_groups));
  537. perf_ibs->attr_groups[0] = &perf_ibs->format_group;
  538. perf_ibs->pmu.attr_groups = perf_ibs->attr_groups;
  539. }
  540. ret = perf_pmu_register(&perf_ibs->pmu, name, -1);
  541. if (ret) {
  542. perf_ibs->pcpu = NULL;
  543. free_percpu(pcpu);
  544. }
  545. return ret;
  546. }
  547. static __init int perf_event_ibs_init(void)
  548. {
  549. struct attribute **attr = ibs_op_format_attrs;
  550. if (!ibs_caps)
  551. return -ENODEV; /* ibs not supported by the cpu */
  552. perf_ibs_pmu_init(&perf_ibs_fetch, "ibs_fetch");
  553. if (ibs_caps & IBS_CAPS_OPCNT) {
  554. perf_ibs_op.config_mask |= IBS_OP_CNT_CTL;
  555. *attr++ = &format_attr_cnt_ctl.attr;
  556. }
  557. perf_ibs_pmu_init(&perf_ibs_op, "ibs_op");
  558. register_nmi_handler(NMI_LOCAL, perf_ibs_nmi_handler, 0, "perf_ibs");
  559. printk(KERN_INFO "perf: AMD IBS detected (0x%08x)\n", ibs_caps);
  560. return 0;
  561. }
  562. #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
  563. static __init int perf_event_ibs_init(void) { return 0; }
  564. #endif
  565. /* IBS - apic initialization, for perf and oprofile */
  566. static __init u32 __get_ibs_caps(void)
  567. {
  568. u32 caps;
  569. unsigned int max_level;
  570. if (!boot_cpu_has(X86_FEATURE_IBS))
  571. return 0;
  572. /* check IBS cpuid feature flags */
  573. max_level = cpuid_eax(0x80000000);
  574. if (max_level < IBS_CPUID_FEATURES)
  575. return IBS_CAPS_DEFAULT;
  576. caps = cpuid_eax(IBS_CPUID_FEATURES);
  577. if (!(caps & IBS_CAPS_AVAIL))
  578. /* cpuid flags not valid */
  579. return IBS_CAPS_DEFAULT;
  580. return caps;
  581. }
  582. u32 get_ibs_caps(void)
  583. {
  584. return ibs_caps;
  585. }
  586. EXPORT_SYMBOL(get_ibs_caps);
  587. static inline int get_eilvt(int offset)
  588. {
  589. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  590. }
  591. static inline int put_eilvt(int offset)
  592. {
  593. return !setup_APIC_eilvt(offset, 0, 0, 1);
  594. }
  595. /*
  596. * Check and reserve APIC extended interrupt LVT offset for IBS if available.
  597. */
  598. static inline int ibs_eilvt_valid(void)
  599. {
  600. int offset;
  601. u64 val;
  602. int valid = 0;
  603. preempt_disable();
  604. rdmsrl(MSR_AMD64_IBSCTL, val);
  605. offset = val & IBSCTL_LVT_OFFSET_MASK;
  606. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  607. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  608. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  609. goto out;
  610. }
  611. if (!get_eilvt(offset)) {
  612. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  613. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  614. goto out;
  615. }
  616. valid = 1;
  617. out:
  618. preempt_enable();
  619. return valid;
  620. }
  621. static int setup_ibs_ctl(int ibs_eilvt_off)
  622. {
  623. struct pci_dev *cpu_cfg;
  624. int nodes;
  625. u32 value = 0;
  626. nodes = 0;
  627. cpu_cfg = NULL;
  628. do {
  629. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  630. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  631. cpu_cfg);
  632. if (!cpu_cfg)
  633. break;
  634. ++nodes;
  635. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  636. | IBSCTL_LVT_OFFSET_VALID);
  637. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  638. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  639. pci_dev_put(cpu_cfg);
  640. printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
  641. "IBSCTL = 0x%08x\n", value);
  642. return -EINVAL;
  643. }
  644. } while (1);
  645. if (!nodes) {
  646. printk(KERN_DEBUG "No CPU node configured for IBS\n");
  647. return -ENODEV;
  648. }
  649. return 0;
  650. }
  651. /*
  652. * This runs only on the current cpu. We try to find an LVT offset and
  653. * setup the local APIC. For this we must disable preemption. On
  654. * success we initialize all nodes with this offset. This updates then
  655. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  656. * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
  657. * is using the new offset.
  658. */
  659. static void force_ibs_eilvt_setup(void)
  660. {
  661. int offset;
  662. int ret;
  663. preempt_disable();
  664. /* find the next free available EILVT entry, skip offset 0 */
  665. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  666. if (get_eilvt(offset))
  667. break;
  668. }
  669. preempt_enable();
  670. if (offset == APIC_EILVT_NR_MAX) {
  671. printk(KERN_DEBUG "No EILVT entry available\n");
  672. return;
  673. }
  674. ret = setup_ibs_ctl(offset);
  675. if (ret)
  676. goto out;
  677. if (!ibs_eilvt_valid())
  678. goto out;
  679. pr_info("IBS: LVT offset %d assigned\n", offset);
  680. return;
  681. out:
  682. preempt_disable();
  683. put_eilvt(offset);
  684. preempt_enable();
  685. return;
  686. }
  687. static void ibs_eilvt_setup(void)
  688. {
  689. /*
  690. * Force LVT offset assignment for family 10h: The offsets are
  691. * not assigned by the BIOS for this family, so the OS is
  692. * responsible for doing it. If the OS assignment fails, fall
  693. * back to BIOS settings and try to setup this.
  694. */
  695. if (boot_cpu_data.x86 == 0x10)
  696. force_ibs_eilvt_setup();
  697. }
  698. static inline int get_ibs_lvt_offset(void)
  699. {
  700. u64 val;
  701. rdmsrl(MSR_AMD64_IBSCTL, val);
  702. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  703. return -EINVAL;
  704. return val & IBSCTL_LVT_OFFSET_MASK;
  705. }
  706. static void setup_APIC_ibs(void *dummy)
  707. {
  708. int offset;
  709. offset = get_ibs_lvt_offset();
  710. if (offset < 0)
  711. goto failed;
  712. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  713. return;
  714. failed:
  715. pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
  716. smp_processor_id());
  717. }
  718. static void clear_APIC_ibs(void *dummy)
  719. {
  720. int offset;
  721. offset = get_ibs_lvt_offset();
  722. if (offset >= 0)
  723. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  724. }
  725. #ifdef CONFIG_PM
  726. static int perf_ibs_suspend(void)
  727. {
  728. clear_APIC_ibs(NULL);
  729. return 0;
  730. }
  731. static void perf_ibs_resume(void)
  732. {
  733. ibs_eilvt_setup();
  734. setup_APIC_ibs(NULL);
  735. }
  736. static struct syscore_ops perf_ibs_syscore_ops = {
  737. .resume = perf_ibs_resume,
  738. .suspend = perf_ibs_suspend,
  739. };
  740. static void perf_ibs_pm_init(void)
  741. {
  742. register_syscore_ops(&perf_ibs_syscore_ops);
  743. }
  744. #else
  745. static inline void perf_ibs_pm_init(void) { }
  746. #endif
  747. static int
  748. perf_ibs_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  749. {
  750. switch (action & ~CPU_TASKS_FROZEN) {
  751. case CPU_STARTING:
  752. setup_APIC_ibs(NULL);
  753. break;
  754. case CPU_DYING:
  755. clear_APIC_ibs(NULL);
  756. break;
  757. default:
  758. break;
  759. }
  760. return NOTIFY_OK;
  761. }
  762. static __init int amd_ibs_init(void)
  763. {
  764. u32 caps;
  765. int ret = -EINVAL;
  766. caps = __get_ibs_caps();
  767. if (!caps)
  768. return -ENODEV; /* ibs not supported by the cpu */
  769. ibs_eilvt_setup();
  770. if (!ibs_eilvt_valid())
  771. goto out;
  772. perf_ibs_pm_init();
  773. cpu_notifier_register_begin();
  774. ibs_caps = caps;
  775. /* make ibs_caps visible to other cpus: */
  776. smp_mb();
  777. smp_call_function(setup_APIC_ibs, NULL, 1);
  778. __perf_cpu_notifier(perf_ibs_cpu_notifier);
  779. cpu_notifier_register_done();
  780. ret = perf_event_ibs_init();
  781. out:
  782. if (ret)
  783. pr_err("Failed to setup IBS, %d\n", ret);
  784. return ret;
  785. }
  786. /* Since we need the pci subsystem to init ibs we can't do this earlier: */
  787. device_initcall(amd_ibs_init);