ibs.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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 <linux/sched/clock.h>
  15. #include <asm/apic.h>
  16. #include "../perf_event.h"
  17. static u32 ibs_caps;
  18. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
  19. #include <linux/kprobes.h>
  20. #include <linux/hardirq.h>
  21. #include <asm/nmi.h>
  22. #include <asm/amd-ibs.h>
  23. #define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)
  24. #define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT
  25. /*
  26. * IBS states:
  27. *
  28. * ENABLED; tracks the pmu::add(), pmu::del() state, when set the counter is taken
  29. * and any further add()s must fail.
  30. *
  31. * STARTED/STOPPING/STOPPED; deal with pmu::start(), pmu::stop() state but are
  32. * complicated by the fact that the IBS hardware can send late NMIs (ie. after
  33. * we've cleared the EN bit).
  34. *
  35. * In order to consume these late NMIs we have the STOPPED state, any NMI that
  36. * happens after we've cleared the EN state will clear this bit and report the
  37. * NMI handled (this is fundamentally racy in the face or multiple NMI sources,
  38. * someone else can consume our BIT and our NMI will go unhandled).
  39. *
  40. * And since we cannot set/clear this separate bit together with the EN bit,
  41. * there are races; if we cleared STARTED early, an NMI could land in
  42. * between clearing STARTED and clearing the EN bit (in fact multiple NMIs
  43. * could happen if the period is small enough), and consume our STOPPED bit
  44. * and trigger streams of unhandled NMIs.
  45. *
  46. * If, however, we clear STARTED late, an NMI can hit between clearing the
  47. * EN bit and clearing STARTED, still see STARTED set and process the event.
  48. * If this event will have the VALID bit clear, we bail properly, but this
  49. * is not a given. With VALID set we can end up calling pmu::stop() again
  50. * (the throttle logic) and trigger the WARNs in there.
  51. *
  52. * So what we do is set STOPPING before clearing EN to avoid the pmu::stop()
  53. * nesting, and clear STARTED late, so that we have a well defined state over
  54. * the clearing of the EN bit.
  55. *
  56. * XXX: we could probably be using !atomic bitops for all this.
  57. */
  58. enum ibs_states {
  59. IBS_ENABLED = 0,
  60. IBS_STARTED = 1,
  61. IBS_STOPPING = 2,
  62. IBS_STOPPED = 3,
  63. IBS_MAX_STATES,
  64. };
  65. struct cpu_perf_ibs {
  66. struct perf_event *event;
  67. unsigned long state[BITS_TO_LONGS(IBS_MAX_STATES)];
  68. };
  69. struct perf_ibs {
  70. struct pmu pmu;
  71. unsigned int msr;
  72. u64 config_mask;
  73. u64 cnt_mask;
  74. u64 enable_mask;
  75. u64 valid_mask;
  76. u64 max_period;
  77. unsigned long offset_mask[1];
  78. int offset_max;
  79. unsigned int fetch_count_reset_broken : 1;
  80. unsigned int fetch_ignore_if_zero_rip : 1;
  81. struct cpu_perf_ibs __percpu *pcpu;
  82. u64 (*get_count)(u64 config);
  83. };
  84. static int
  85. perf_event_set_period(struct hw_perf_event *hwc, u64 min, u64 max, u64 *hw_period)
  86. {
  87. s64 left = local64_read(&hwc->period_left);
  88. s64 period = hwc->sample_period;
  89. int overflow = 0;
  90. /*
  91. * If we are way outside a reasonable range then just skip forward:
  92. */
  93. if (unlikely(left <= -period)) {
  94. left = period;
  95. local64_set(&hwc->period_left, left);
  96. hwc->last_period = period;
  97. overflow = 1;
  98. }
  99. if (unlikely(left < (s64)min)) {
  100. left += period;
  101. local64_set(&hwc->period_left, left);
  102. hwc->last_period = period;
  103. overflow = 1;
  104. }
  105. /*
  106. * If the hw period that triggers the sw overflow is too short
  107. * we might hit the irq handler. This biases the results.
  108. * Thus we shorten the next-to-last period and set the last
  109. * period to the max period.
  110. */
  111. if (left > max) {
  112. left -= max;
  113. if (left > max)
  114. left = max;
  115. else if (left < min)
  116. left = min;
  117. }
  118. *hw_period = (u64)left;
  119. return overflow;
  120. }
  121. static int
  122. perf_event_try_update(struct perf_event *event, u64 new_raw_count, int width)
  123. {
  124. struct hw_perf_event *hwc = &event->hw;
  125. int shift = 64 - width;
  126. u64 prev_raw_count;
  127. u64 delta;
  128. /*
  129. * Careful: an NMI might modify the previous event value.
  130. *
  131. * Our tactic to handle this is to first atomically read and
  132. * exchange a new raw count - then add that new-prev delta
  133. * count to the generic event atomically:
  134. */
  135. prev_raw_count = local64_read(&hwc->prev_count);
  136. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  137. new_raw_count) != prev_raw_count)
  138. return 0;
  139. /*
  140. * Now we have the new raw value and have updated the prev
  141. * timestamp already. We can now calculate the elapsed delta
  142. * (event-)time and add that to the generic event.
  143. *
  144. * Careful, not all hw sign-extends above the physical width
  145. * of the count.
  146. */
  147. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  148. delta >>= shift;
  149. local64_add(delta, &event->count);
  150. local64_sub(delta, &hwc->period_left);
  151. return 1;
  152. }
  153. static struct perf_ibs perf_ibs_fetch;
  154. static struct perf_ibs perf_ibs_op;
  155. static struct perf_ibs *get_ibs_pmu(int type)
  156. {
  157. if (perf_ibs_fetch.pmu.type == type)
  158. return &perf_ibs_fetch;
  159. if (perf_ibs_op.pmu.type == type)
  160. return &perf_ibs_op;
  161. return NULL;
  162. }
  163. /*
  164. * core pmu config -> IBS config
  165. *
  166. * perf record -a -e cpu-cycles:p ... # use ibs op counting cycle count
  167. * perf record -a -e r076:p ... # same as -e cpu-cycles:p
  168. * perf record -a -e r0C1:p ... # use ibs op counting micro-ops
  169. *
  170. * IbsOpCntCtl (bit 19) of IBS Execution Control Register (IbsOpCtl,
  171. * MSRC001_1033) is used to select either cycle or micro-ops counting
  172. * mode.
  173. */
  174. static int core_pmu_ibs_config(struct perf_event *event, u64 *config)
  175. {
  176. switch (event->attr.type) {
  177. case PERF_TYPE_HARDWARE:
  178. switch (event->attr.config) {
  179. case PERF_COUNT_HW_CPU_CYCLES:
  180. *config = 0;
  181. return 0;
  182. }
  183. break;
  184. case PERF_TYPE_RAW:
  185. switch (event->attr.config) {
  186. case 0x0076:
  187. *config = 0;
  188. return 0;
  189. case 0x00C1:
  190. *config = IBS_OP_CNT_CTL;
  191. return 0;
  192. }
  193. break;
  194. default:
  195. return -ENOENT;
  196. }
  197. return -EOPNOTSUPP;
  198. }
  199. /*
  200. * The rip of IBS samples has skid 0. Thus, IBS supports precise
  201. * levels 1 and 2 and the PERF_EFLAGS_EXACT is set. In rare cases the
  202. * rip is invalid when IBS was not able to record the rip correctly.
  203. * We clear PERF_EFLAGS_EXACT and take the rip from pt_regs then.
  204. */
  205. int forward_event_to_ibs(struct perf_event *event)
  206. {
  207. u64 config = 0;
  208. if (!event->attr.precise_ip || event->attr.precise_ip > 2)
  209. return -EOPNOTSUPP;
  210. if (!core_pmu_ibs_config(event, &config)) {
  211. event->attr.type = perf_ibs_op.pmu.type;
  212. event->attr.config = config;
  213. }
  214. return -ENOENT;
  215. }
  216. static int perf_ibs_init(struct perf_event *event)
  217. {
  218. struct hw_perf_event *hwc = &event->hw;
  219. struct perf_ibs *perf_ibs;
  220. u64 max_cnt, config;
  221. perf_ibs = get_ibs_pmu(event->attr.type);
  222. if (!perf_ibs)
  223. return -ENOENT;
  224. config = event->attr.config;
  225. if (event->pmu != &perf_ibs->pmu)
  226. return -ENOENT;
  227. if (config & ~perf_ibs->config_mask)
  228. return -EINVAL;
  229. if (hwc->sample_period) {
  230. if (config & perf_ibs->cnt_mask)
  231. /* raw max_cnt may not be set */
  232. return -EINVAL;
  233. if (!event->attr.sample_freq && hwc->sample_period & 0x0f)
  234. /*
  235. * lower 4 bits can not be set in ibs max cnt,
  236. * but allowing it in case we adjust the
  237. * sample period to set a frequency.
  238. */
  239. return -EINVAL;
  240. hwc->sample_period &= ~0x0FULL;
  241. if (!hwc->sample_period)
  242. hwc->sample_period = 0x10;
  243. } else {
  244. max_cnt = config & perf_ibs->cnt_mask;
  245. config &= ~perf_ibs->cnt_mask;
  246. event->attr.sample_period = max_cnt << 4;
  247. hwc->sample_period = event->attr.sample_period;
  248. }
  249. if (!hwc->sample_period)
  250. return -EINVAL;
  251. /*
  252. * If we modify hwc->sample_period, we also need to update
  253. * hwc->last_period and hwc->period_left.
  254. */
  255. hwc->last_period = hwc->sample_period;
  256. local64_set(&hwc->period_left, hwc->sample_period);
  257. hwc->config_base = perf_ibs->msr;
  258. hwc->config = config;
  259. return 0;
  260. }
  261. static int perf_ibs_set_period(struct perf_ibs *perf_ibs,
  262. struct hw_perf_event *hwc, u64 *period)
  263. {
  264. int overflow;
  265. /* ignore lower 4 bits in min count: */
  266. overflow = perf_event_set_period(hwc, 1<<4, perf_ibs->max_period, period);
  267. local64_set(&hwc->prev_count, 0);
  268. return overflow;
  269. }
  270. static u64 get_ibs_fetch_count(u64 config)
  271. {
  272. union ibs_fetch_ctl fetch_ctl = (union ibs_fetch_ctl)config;
  273. return fetch_ctl.fetch_cnt << 4;
  274. }
  275. static u64 get_ibs_op_count(u64 config)
  276. {
  277. union ibs_op_ctl op_ctl = (union ibs_op_ctl)config;
  278. u64 count = 0;
  279. /*
  280. * If the internal 27-bit counter rolled over, the count is MaxCnt
  281. * and the lower 7 bits of CurCnt are randomized.
  282. * Otherwise CurCnt has the full 27-bit current counter value.
  283. */
  284. if (op_ctl.op_val) {
  285. count = op_ctl.opmaxcnt << 4;
  286. if (ibs_caps & IBS_CAPS_OPCNTEXT)
  287. count += op_ctl.opmaxcnt_ext << 20;
  288. } else if (ibs_caps & IBS_CAPS_RDWROPCNT) {
  289. count = op_ctl.opcurcnt;
  290. }
  291. return count;
  292. }
  293. static void
  294. perf_ibs_event_update(struct perf_ibs *perf_ibs, struct perf_event *event,
  295. u64 *config)
  296. {
  297. u64 count = perf_ibs->get_count(*config);
  298. /*
  299. * Set width to 64 since we do not overflow on max width but
  300. * instead on max count. In perf_ibs_set_period() we clear
  301. * prev count manually on overflow.
  302. */
  303. while (!perf_event_try_update(event, count, 64)) {
  304. rdmsrl(event->hw.config_base, *config);
  305. count = perf_ibs->get_count(*config);
  306. }
  307. }
  308. static inline void perf_ibs_enable_event(struct perf_ibs *perf_ibs,
  309. struct hw_perf_event *hwc, u64 config)
  310. {
  311. u64 tmp = hwc->config | config;
  312. if (perf_ibs->fetch_count_reset_broken)
  313. wrmsrl(hwc->config_base, tmp & ~perf_ibs->enable_mask);
  314. wrmsrl(hwc->config_base, tmp | perf_ibs->enable_mask);
  315. }
  316. /*
  317. * Erratum #420 Instruction-Based Sampling Engine May Generate
  318. * Interrupt that Cannot Be Cleared:
  319. *
  320. * Must clear counter mask first, then clear the enable bit. See
  321. * Revision Guide for AMD Family 10h Processors, Publication #41322.
  322. */
  323. static inline void perf_ibs_disable_event(struct perf_ibs *perf_ibs,
  324. struct hw_perf_event *hwc, u64 config)
  325. {
  326. config &= ~perf_ibs->cnt_mask;
  327. if (boot_cpu_data.x86 == 0x10)
  328. wrmsrl(hwc->config_base, config);
  329. config &= ~perf_ibs->enable_mask;
  330. wrmsrl(hwc->config_base, config);
  331. }
  332. /*
  333. * We cannot restore the ibs pmu state, so we always needs to update
  334. * the event while stopping it and then reset the state when starting
  335. * again. Thus, ignoring PERF_EF_RELOAD and PERF_EF_UPDATE flags in
  336. * perf_ibs_start()/perf_ibs_stop() and instead always do it.
  337. */
  338. static void perf_ibs_start(struct perf_event *event, int flags)
  339. {
  340. struct hw_perf_event *hwc = &event->hw;
  341. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  342. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  343. u64 period, config = 0;
  344. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  345. return;
  346. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  347. hwc->state = 0;
  348. perf_ibs_set_period(perf_ibs, hwc, &period);
  349. if (perf_ibs == &perf_ibs_op && (ibs_caps & IBS_CAPS_OPCNTEXT)) {
  350. config |= period & IBS_OP_MAX_CNT_EXT_MASK;
  351. period &= ~IBS_OP_MAX_CNT_EXT_MASK;
  352. }
  353. config |= period >> 4;
  354. /*
  355. * Set STARTED before enabling the hardware, such that a subsequent NMI
  356. * must observe it.
  357. */
  358. set_bit(IBS_STARTED, pcpu->state);
  359. clear_bit(IBS_STOPPING, pcpu->state);
  360. perf_ibs_enable_event(perf_ibs, hwc, config);
  361. perf_event_update_userpage(event);
  362. }
  363. static void perf_ibs_stop(struct perf_event *event, int flags)
  364. {
  365. struct hw_perf_event *hwc = &event->hw;
  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. u64 config;
  369. int stopping;
  370. if (test_and_set_bit(IBS_STOPPING, pcpu->state))
  371. return;
  372. stopping = test_bit(IBS_STARTED, pcpu->state);
  373. if (!stopping && (hwc->state & PERF_HES_UPTODATE))
  374. return;
  375. rdmsrl(hwc->config_base, config);
  376. if (stopping) {
  377. /*
  378. * Set STOPPED before disabling the hardware, such that it
  379. * must be visible to NMIs the moment we clear the EN bit,
  380. * at which point we can generate an !VALID sample which
  381. * we need to consume.
  382. */
  383. set_bit(IBS_STOPPED, pcpu->state);
  384. perf_ibs_disable_event(perf_ibs, hwc, config);
  385. /*
  386. * Clear STARTED after disabling the hardware; if it were
  387. * cleared before an NMI hitting after the clear but before
  388. * clearing the EN bit might think it a spurious NMI and not
  389. * handle it.
  390. *
  391. * Clearing it after, however, creates the problem of the NMI
  392. * handler seeing STARTED but not having a valid sample.
  393. */
  394. clear_bit(IBS_STARTED, pcpu->state);
  395. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  396. hwc->state |= PERF_HES_STOPPED;
  397. }
  398. if (hwc->state & PERF_HES_UPTODATE)
  399. return;
  400. /*
  401. * Clear valid bit to not count rollovers on update, rollovers
  402. * are only updated in the irq handler.
  403. */
  404. config &= ~perf_ibs->valid_mask;
  405. perf_ibs_event_update(perf_ibs, event, &config);
  406. hwc->state |= PERF_HES_UPTODATE;
  407. }
  408. static int perf_ibs_add(struct perf_event *event, int flags)
  409. {
  410. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  411. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  412. if (test_and_set_bit(IBS_ENABLED, pcpu->state))
  413. return -ENOSPC;
  414. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  415. pcpu->event = event;
  416. if (flags & PERF_EF_START)
  417. perf_ibs_start(event, PERF_EF_RELOAD);
  418. return 0;
  419. }
  420. static void perf_ibs_del(struct perf_event *event, int flags)
  421. {
  422. struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
  423. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  424. if (!test_and_clear_bit(IBS_ENABLED, pcpu->state))
  425. return;
  426. perf_ibs_stop(event, PERF_EF_UPDATE);
  427. pcpu->event = NULL;
  428. perf_event_update_userpage(event);
  429. }
  430. static void perf_ibs_read(struct perf_event *event) { }
  431. /*
  432. * We need to initialize with empty group if all attributes in the
  433. * group are dynamic.
  434. */
  435. static struct attribute *attrs_empty[] = {
  436. NULL,
  437. };
  438. static struct attribute_group empty_format_group = {
  439. .name = "format",
  440. .attrs = attrs_empty,
  441. };
  442. static struct attribute_group empty_caps_group = {
  443. .name = "caps",
  444. .attrs = attrs_empty,
  445. };
  446. static const struct attribute_group *empty_attr_groups[] = {
  447. &empty_format_group,
  448. &empty_caps_group,
  449. NULL,
  450. };
  451. PMU_FORMAT_ATTR(rand_en, "config:57");
  452. PMU_FORMAT_ATTR(cnt_ctl, "config:19");
  453. PMU_EVENT_ATTR_STRING(l3missonly, fetch_l3missonly, "config:59");
  454. PMU_EVENT_ATTR_STRING(l3missonly, op_l3missonly, "config:16");
  455. PMU_EVENT_ATTR_STRING(zen4_ibs_extensions, zen4_ibs_extensions, "1");
  456. static umode_t
  457. zen4_ibs_extensions_is_visible(struct kobject *kobj, struct attribute *attr, int i)
  458. {
  459. return ibs_caps & IBS_CAPS_ZEN4 ? attr->mode : 0;
  460. }
  461. static struct attribute *rand_en_attrs[] = {
  462. &format_attr_rand_en.attr,
  463. NULL,
  464. };
  465. static struct attribute *fetch_l3missonly_attrs[] = {
  466. &fetch_l3missonly.attr.attr,
  467. NULL,
  468. };
  469. static struct attribute *zen4_ibs_extensions_attrs[] = {
  470. &zen4_ibs_extensions.attr.attr,
  471. NULL,
  472. };
  473. static struct attribute_group group_rand_en = {
  474. .name = "format",
  475. .attrs = rand_en_attrs,
  476. };
  477. static struct attribute_group group_fetch_l3missonly = {
  478. .name = "format",
  479. .attrs = fetch_l3missonly_attrs,
  480. .is_visible = zen4_ibs_extensions_is_visible,
  481. };
  482. static struct attribute_group group_zen4_ibs_extensions = {
  483. .name = "caps",
  484. .attrs = zen4_ibs_extensions_attrs,
  485. .is_visible = zen4_ibs_extensions_is_visible,
  486. };
  487. static const struct attribute_group *fetch_attr_groups[] = {
  488. &group_rand_en,
  489. &empty_caps_group,
  490. NULL,
  491. };
  492. static const struct attribute_group *fetch_attr_update[] = {
  493. &group_fetch_l3missonly,
  494. &group_zen4_ibs_extensions,
  495. NULL,
  496. };
  497. static umode_t
  498. cnt_ctl_is_visible(struct kobject *kobj, struct attribute *attr, int i)
  499. {
  500. return ibs_caps & IBS_CAPS_OPCNT ? attr->mode : 0;
  501. }
  502. static struct attribute *cnt_ctl_attrs[] = {
  503. &format_attr_cnt_ctl.attr,
  504. NULL,
  505. };
  506. static struct attribute *op_l3missonly_attrs[] = {
  507. &op_l3missonly.attr.attr,
  508. NULL,
  509. };
  510. static struct attribute_group group_cnt_ctl = {
  511. .name = "format",
  512. .attrs = cnt_ctl_attrs,
  513. .is_visible = cnt_ctl_is_visible,
  514. };
  515. static struct attribute_group group_op_l3missonly = {
  516. .name = "format",
  517. .attrs = op_l3missonly_attrs,
  518. .is_visible = zen4_ibs_extensions_is_visible,
  519. };
  520. static const struct attribute_group *op_attr_update[] = {
  521. &group_cnt_ctl,
  522. &group_op_l3missonly,
  523. &group_zen4_ibs_extensions,
  524. NULL,
  525. };
  526. static struct perf_ibs perf_ibs_fetch = {
  527. .pmu = {
  528. .task_ctx_nr = perf_invalid_context,
  529. .event_init = perf_ibs_init,
  530. .add = perf_ibs_add,
  531. .del = perf_ibs_del,
  532. .start = perf_ibs_start,
  533. .stop = perf_ibs_stop,
  534. .read = perf_ibs_read,
  535. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  536. },
  537. .msr = MSR_AMD64_IBSFETCHCTL,
  538. .config_mask = IBS_FETCH_CONFIG_MASK,
  539. .cnt_mask = IBS_FETCH_MAX_CNT,
  540. .enable_mask = IBS_FETCH_ENABLE,
  541. .valid_mask = IBS_FETCH_VAL,
  542. .max_period = IBS_FETCH_MAX_CNT << 4,
  543. .offset_mask = { MSR_AMD64_IBSFETCH_REG_MASK },
  544. .offset_max = MSR_AMD64_IBSFETCH_REG_COUNT,
  545. .get_count = get_ibs_fetch_count,
  546. };
  547. static struct perf_ibs perf_ibs_op = {
  548. .pmu = {
  549. .task_ctx_nr = perf_invalid_context,
  550. .event_init = perf_ibs_init,
  551. .add = perf_ibs_add,
  552. .del = perf_ibs_del,
  553. .start = perf_ibs_start,
  554. .stop = perf_ibs_stop,
  555. .read = perf_ibs_read,
  556. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  557. },
  558. .msr = MSR_AMD64_IBSOPCTL,
  559. .config_mask = IBS_OP_CONFIG_MASK,
  560. .cnt_mask = IBS_OP_MAX_CNT | IBS_OP_CUR_CNT |
  561. IBS_OP_CUR_CNT_RAND,
  562. .enable_mask = IBS_OP_ENABLE,
  563. .valid_mask = IBS_OP_VAL,
  564. .max_period = IBS_OP_MAX_CNT << 4,
  565. .offset_mask = { MSR_AMD64_IBSOP_REG_MASK },
  566. .offset_max = MSR_AMD64_IBSOP_REG_COUNT,
  567. .get_count = get_ibs_op_count,
  568. };
  569. static void perf_ibs_get_mem_op(union ibs_op_data3 *op_data3,
  570. struct perf_sample_data *data)
  571. {
  572. union perf_mem_data_src *data_src = &data->data_src;
  573. data_src->mem_op = PERF_MEM_OP_NA;
  574. if (op_data3->ld_op)
  575. data_src->mem_op = PERF_MEM_OP_LOAD;
  576. else if (op_data3->st_op)
  577. data_src->mem_op = PERF_MEM_OP_STORE;
  578. }
  579. /*
  580. * Processors having CPUID_Fn8000001B_EAX[11] aka IBS_CAPS_ZEN4 has
  581. * more fine granular DataSrc encodings. Others have coarse.
  582. */
  583. static u8 perf_ibs_data_src(union ibs_op_data2 *op_data2)
  584. {
  585. if (ibs_caps & IBS_CAPS_ZEN4)
  586. return (op_data2->data_src_hi << 3) | op_data2->data_src_lo;
  587. return op_data2->data_src_lo;
  588. }
  589. static void perf_ibs_get_mem_lvl(union ibs_op_data2 *op_data2,
  590. union ibs_op_data3 *op_data3,
  591. struct perf_sample_data *data)
  592. {
  593. union perf_mem_data_src *data_src = &data->data_src;
  594. u8 ibs_data_src = perf_ibs_data_src(op_data2);
  595. data_src->mem_lvl = 0;
  596. /*
  597. * DcMiss, L2Miss, DataSrc, DcMissLat etc. are all invalid for Uncached
  598. * memory accesses. So, check DcUcMemAcc bit early.
  599. */
  600. if (op_data3->dc_uc_mem_acc && ibs_data_src != IBS_DATA_SRC_EXT_IO) {
  601. data_src->mem_lvl = PERF_MEM_LVL_UNC | PERF_MEM_LVL_HIT;
  602. return;
  603. }
  604. /* L1 Hit */
  605. if (op_data3->dc_miss == 0) {
  606. data_src->mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
  607. return;
  608. }
  609. /* L2 Hit */
  610. if (op_data3->l2_miss == 0) {
  611. /* Erratum #1293 */
  612. if (boot_cpu_data.x86 != 0x19 || boot_cpu_data.x86_model > 0xF ||
  613. !(op_data3->sw_pf || op_data3->dc_miss_no_mab_alloc)) {
  614. data_src->mem_lvl = PERF_MEM_LVL_L2 | PERF_MEM_LVL_HIT;
  615. return;
  616. }
  617. }
  618. /*
  619. * OP_DATA2 is valid only for load ops. Skip all checks which
  620. * uses OP_DATA2[DataSrc].
  621. */
  622. if (data_src->mem_op != PERF_MEM_OP_LOAD)
  623. goto check_mab;
  624. /* L3 Hit */
  625. if (ibs_caps & IBS_CAPS_ZEN4) {
  626. if (ibs_data_src == IBS_DATA_SRC_EXT_LOC_CACHE) {
  627. data_src->mem_lvl = PERF_MEM_LVL_L3 | PERF_MEM_LVL_HIT;
  628. return;
  629. }
  630. } else {
  631. if (ibs_data_src == IBS_DATA_SRC_LOC_CACHE) {
  632. data_src->mem_lvl = PERF_MEM_LVL_L3 | PERF_MEM_LVL_REM_CCE1 |
  633. PERF_MEM_LVL_HIT;
  634. return;
  635. }
  636. }
  637. /* A peer cache in a near CCX */
  638. if (ibs_caps & IBS_CAPS_ZEN4 &&
  639. ibs_data_src == IBS_DATA_SRC_EXT_NEAR_CCX_CACHE) {
  640. data_src->mem_lvl = PERF_MEM_LVL_REM_CCE1 | PERF_MEM_LVL_HIT;
  641. return;
  642. }
  643. /* A peer cache in a far CCX */
  644. if (ibs_caps & IBS_CAPS_ZEN4) {
  645. if (ibs_data_src == IBS_DATA_SRC_EXT_FAR_CCX_CACHE) {
  646. data_src->mem_lvl = PERF_MEM_LVL_REM_CCE2 | PERF_MEM_LVL_HIT;
  647. return;
  648. }
  649. } else {
  650. if (ibs_data_src == IBS_DATA_SRC_REM_CACHE) {
  651. data_src->mem_lvl = PERF_MEM_LVL_REM_CCE2 | PERF_MEM_LVL_HIT;
  652. return;
  653. }
  654. }
  655. /* DRAM */
  656. if (ibs_data_src == IBS_DATA_SRC_EXT_DRAM) {
  657. if (op_data2->rmt_node == 0)
  658. data_src->mem_lvl = PERF_MEM_LVL_LOC_RAM | PERF_MEM_LVL_HIT;
  659. else
  660. data_src->mem_lvl = PERF_MEM_LVL_REM_RAM1 | PERF_MEM_LVL_HIT;
  661. return;
  662. }
  663. /* PMEM */
  664. if (ibs_caps & IBS_CAPS_ZEN4 && ibs_data_src == IBS_DATA_SRC_EXT_PMEM) {
  665. data_src->mem_lvl_num = PERF_MEM_LVLNUM_PMEM;
  666. if (op_data2->rmt_node) {
  667. data_src->mem_remote = PERF_MEM_REMOTE_REMOTE;
  668. /* IBS doesn't provide Remote socket detail */
  669. data_src->mem_hops = PERF_MEM_HOPS_1;
  670. }
  671. return;
  672. }
  673. /* Extension Memory */
  674. if (ibs_caps & IBS_CAPS_ZEN4 &&
  675. ibs_data_src == IBS_DATA_SRC_EXT_EXT_MEM) {
  676. data_src->mem_lvl_num = PERF_MEM_LVLNUM_CXL;
  677. if (op_data2->rmt_node) {
  678. data_src->mem_remote = PERF_MEM_REMOTE_REMOTE;
  679. /* IBS doesn't provide Remote socket detail */
  680. data_src->mem_hops = PERF_MEM_HOPS_1;
  681. }
  682. return;
  683. }
  684. /* IO */
  685. if (ibs_data_src == IBS_DATA_SRC_EXT_IO) {
  686. data_src->mem_lvl = PERF_MEM_LVL_IO;
  687. data_src->mem_lvl_num = PERF_MEM_LVLNUM_IO;
  688. if (op_data2->rmt_node) {
  689. data_src->mem_remote = PERF_MEM_REMOTE_REMOTE;
  690. /* IBS doesn't provide Remote socket detail */
  691. data_src->mem_hops = PERF_MEM_HOPS_1;
  692. }
  693. return;
  694. }
  695. check_mab:
  696. /*
  697. * MAB (Miss Address Buffer) Hit. MAB keeps track of outstanding
  698. * DC misses. However, such data may come from any level in mem
  699. * hierarchy. IBS provides detail about both MAB as well as actual
  700. * DataSrc simultaneously. Prioritize DataSrc over MAB, i.e. set
  701. * MAB only when IBS fails to provide DataSrc.
  702. */
  703. if (op_data3->dc_miss_no_mab_alloc) {
  704. data_src->mem_lvl = PERF_MEM_LVL_LFB | PERF_MEM_LVL_HIT;
  705. return;
  706. }
  707. data_src->mem_lvl = PERF_MEM_LVL_NA;
  708. }
  709. static bool perf_ibs_cache_hit_st_valid(void)
  710. {
  711. /* 0: Uninitialized, 1: Valid, -1: Invalid */
  712. static int cache_hit_st_valid;
  713. if (unlikely(!cache_hit_st_valid)) {
  714. if (boot_cpu_data.x86 == 0x19 &&
  715. (boot_cpu_data.x86_model <= 0xF ||
  716. (boot_cpu_data.x86_model >= 0x20 &&
  717. boot_cpu_data.x86_model <= 0x5F))) {
  718. cache_hit_st_valid = -1;
  719. } else {
  720. cache_hit_st_valid = 1;
  721. }
  722. }
  723. return cache_hit_st_valid == 1;
  724. }
  725. static void perf_ibs_get_mem_snoop(union ibs_op_data2 *op_data2,
  726. struct perf_sample_data *data)
  727. {
  728. union perf_mem_data_src *data_src = &data->data_src;
  729. u8 ibs_data_src;
  730. data_src->mem_snoop = PERF_MEM_SNOOP_NA;
  731. if (!perf_ibs_cache_hit_st_valid() ||
  732. data_src->mem_op != PERF_MEM_OP_LOAD ||
  733. data_src->mem_lvl & PERF_MEM_LVL_L1 ||
  734. data_src->mem_lvl & PERF_MEM_LVL_L2 ||
  735. op_data2->cache_hit_st)
  736. return;
  737. ibs_data_src = perf_ibs_data_src(op_data2);
  738. if (ibs_caps & IBS_CAPS_ZEN4) {
  739. if (ibs_data_src == IBS_DATA_SRC_EXT_LOC_CACHE ||
  740. ibs_data_src == IBS_DATA_SRC_EXT_NEAR_CCX_CACHE ||
  741. ibs_data_src == IBS_DATA_SRC_EXT_FAR_CCX_CACHE)
  742. data_src->mem_snoop = PERF_MEM_SNOOP_HITM;
  743. } else if (ibs_data_src == IBS_DATA_SRC_LOC_CACHE) {
  744. data_src->mem_snoop = PERF_MEM_SNOOP_HITM;
  745. }
  746. }
  747. static void perf_ibs_get_tlb_lvl(union ibs_op_data3 *op_data3,
  748. struct perf_sample_data *data)
  749. {
  750. union perf_mem_data_src *data_src = &data->data_src;
  751. data_src->mem_dtlb = PERF_MEM_TLB_NA;
  752. if (!op_data3->dc_lin_addr_valid)
  753. return;
  754. if (!op_data3->dc_l1tlb_miss) {
  755. data_src->mem_dtlb = PERF_MEM_TLB_L1 | PERF_MEM_TLB_HIT;
  756. return;
  757. }
  758. if (!op_data3->dc_l2tlb_miss) {
  759. data_src->mem_dtlb = PERF_MEM_TLB_L2 | PERF_MEM_TLB_HIT;
  760. return;
  761. }
  762. data_src->mem_dtlb = PERF_MEM_TLB_L2 | PERF_MEM_TLB_MISS;
  763. }
  764. static void perf_ibs_get_mem_lock(union ibs_op_data3 *op_data3,
  765. struct perf_sample_data *data)
  766. {
  767. union perf_mem_data_src *data_src = &data->data_src;
  768. data_src->mem_lock = PERF_MEM_LOCK_NA;
  769. if (op_data3->dc_locked_op)
  770. data_src->mem_lock = PERF_MEM_LOCK_LOCKED;
  771. }
  772. #define ibs_op_msr_idx(msr) (msr - MSR_AMD64_IBSOPCTL)
  773. static void perf_ibs_get_data_src(struct perf_ibs_data *ibs_data,
  774. struct perf_sample_data *data,
  775. union ibs_op_data2 *op_data2,
  776. union ibs_op_data3 *op_data3)
  777. {
  778. perf_ibs_get_mem_lvl(op_data2, op_data3, data);
  779. perf_ibs_get_mem_snoop(op_data2, data);
  780. perf_ibs_get_tlb_lvl(op_data3, data);
  781. perf_ibs_get_mem_lock(op_data3, data);
  782. }
  783. static __u64 perf_ibs_get_op_data2(struct perf_ibs_data *ibs_data,
  784. union ibs_op_data3 *op_data3)
  785. {
  786. __u64 val = ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSOPDATA2)];
  787. /* Erratum #1293 */
  788. if (boot_cpu_data.x86 == 0x19 && boot_cpu_data.x86_model <= 0xF &&
  789. (op_data3->sw_pf || op_data3->dc_miss_no_mab_alloc)) {
  790. /*
  791. * OP_DATA2 has only two fields on Zen3: DataSrc and RmtNode.
  792. * DataSrc=0 is 'No valid status' and RmtNode is invalid when
  793. * DataSrc=0.
  794. */
  795. val = 0;
  796. }
  797. return val;
  798. }
  799. static void perf_ibs_parse_ld_st_data(__u64 sample_type,
  800. struct perf_ibs_data *ibs_data,
  801. struct perf_sample_data *data)
  802. {
  803. union ibs_op_data3 op_data3;
  804. union ibs_op_data2 op_data2;
  805. union ibs_op_data op_data;
  806. data->data_src.val = PERF_MEM_NA;
  807. op_data3.val = ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSOPDATA3)];
  808. perf_ibs_get_mem_op(&op_data3, data);
  809. if (data->data_src.mem_op != PERF_MEM_OP_LOAD &&
  810. data->data_src.mem_op != PERF_MEM_OP_STORE)
  811. return;
  812. op_data2.val = perf_ibs_get_op_data2(ibs_data, &op_data3);
  813. if (sample_type & PERF_SAMPLE_DATA_SRC) {
  814. perf_ibs_get_data_src(ibs_data, data, &op_data2, &op_data3);
  815. data->sample_flags |= PERF_SAMPLE_DATA_SRC;
  816. }
  817. if (sample_type & PERF_SAMPLE_WEIGHT_TYPE && op_data3.dc_miss &&
  818. data->data_src.mem_op == PERF_MEM_OP_LOAD) {
  819. op_data.val = ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSOPDATA)];
  820. if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) {
  821. data->weight.var1_dw = op_data3.dc_miss_lat;
  822. data->weight.var2_w = op_data.tag_to_ret_ctr;
  823. } else if (sample_type & PERF_SAMPLE_WEIGHT) {
  824. data->weight.full = op_data3.dc_miss_lat;
  825. }
  826. data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE;
  827. }
  828. if (sample_type & PERF_SAMPLE_ADDR && op_data3.dc_lin_addr_valid) {
  829. data->addr = ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSDCLINAD)];
  830. data->sample_flags |= PERF_SAMPLE_ADDR;
  831. }
  832. if (sample_type & PERF_SAMPLE_PHYS_ADDR && op_data3.dc_phy_addr_valid) {
  833. data->phys_addr = ibs_data->regs[ibs_op_msr_idx(MSR_AMD64_IBSDCPHYSAD)];
  834. data->sample_flags |= PERF_SAMPLE_PHYS_ADDR;
  835. }
  836. }
  837. static int perf_ibs_get_offset_max(struct perf_ibs *perf_ibs, u64 sample_type,
  838. int check_rip)
  839. {
  840. if (sample_type & PERF_SAMPLE_RAW ||
  841. (perf_ibs == &perf_ibs_op &&
  842. (sample_type & PERF_SAMPLE_DATA_SRC ||
  843. sample_type & PERF_SAMPLE_WEIGHT_TYPE ||
  844. sample_type & PERF_SAMPLE_ADDR ||
  845. sample_type & PERF_SAMPLE_PHYS_ADDR)))
  846. return perf_ibs->offset_max;
  847. else if (check_rip)
  848. return 3;
  849. return 1;
  850. }
  851. static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
  852. {
  853. struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
  854. struct perf_event *event = pcpu->event;
  855. struct hw_perf_event *hwc;
  856. struct perf_sample_data data;
  857. struct perf_raw_record raw;
  858. struct pt_regs regs;
  859. struct perf_ibs_data ibs_data;
  860. int offset, size, check_rip, offset_max, throttle = 0;
  861. unsigned int msr;
  862. u64 *buf, *config, period, new_config = 0;
  863. if (!test_bit(IBS_STARTED, pcpu->state)) {
  864. fail:
  865. /*
  866. * Catch spurious interrupts after stopping IBS: After
  867. * disabling IBS there could be still incoming NMIs
  868. * with samples that even have the valid bit cleared.
  869. * Mark all this NMIs as handled.
  870. */
  871. if (test_and_clear_bit(IBS_STOPPED, pcpu->state))
  872. return 1;
  873. return 0;
  874. }
  875. if (WARN_ON_ONCE(!event))
  876. goto fail;
  877. hwc = &event->hw;
  878. msr = hwc->config_base;
  879. buf = ibs_data.regs;
  880. rdmsrl(msr, *buf);
  881. if (!(*buf++ & perf_ibs->valid_mask))
  882. goto fail;
  883. config = &ibs_data.regs[0];
  884. perf_ibs_event_update(perf_ibs, event, config);
  885. perf_sample_data_init(&data, 0, hwc->last_period);
  886. if (!perf_ibs_set_period(perf_ibs, hwc, &period))
  887. goto out; /* no sw counter overflow */
  888. ibs_data.caps = ibs_caps;
  889. size = 1;
  890. offset = 1;
  891. check_rip = (perf_ibs == &perf_ibs_op && (ibs_caps & IBS_CAPS_RIPINVALIDCHK));
  892. offset_max = perf_ibs_get_offset_max(perf_ibs, event->attr.sample_type, check_rip);
  893. do {
  894. rdmsrl(msr + offset, *buf++);
  895. size++;
  896. offset = find_next_bit(perf_ibs->offset_mask,
  897. perf_ibs->offset_max,
  898. offset + 1);
  899. } while (offset < offset_max);
  900. /*
  901. * Read IbsBrTarget, IbsOpData4, and IbsExtdCtl separately
  902. * depending on their availability.
  903. * Can't add to offset_max as they are staggered
  904. */
  905. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  906. if (perf_ibs == &perf_ibs_op) {
  907. if (ibs_caps & IBS_CAPS_BRNTRGT) {
  908. rdmsrl(MSR_AMD64_IBSBRTARGET, *buf++);
  909. size++;
  910. }
  911. if (ibs_caps & IBS_CAPS_OPDATA4) {
  912. rdmsrl(MSR_AMD64_IBSOPDATA4, *buf++);
  913. size++;
  914. }
  915. }
  916. if (perf_ibs == &perf_ibs_fetch && (ibs_caps & IBS_CAPS_FETCHCTLEXTD)) {
  917. rdmsrl(MSR_AMD64_ICIBSEXTDCTL, *buf++);
  918. size++;
  919. }
  920. }
  921. ibs_data.size = sizeof(u64) * size;
  922. regs = *iregs;
  923. if (check_rip && (ibs_data.regs[2] & IBS_RIP_INVALID)) {
  924. regs.flags &= ~PERF_EFLAGS_EXACT;
  925. } else {
  926. /* Workaround for erratum #1197 */
  927. if (perf_ibs->fetch_ignore_if_zero_rip && !(ibs_data.regs[1]))
  928. goto out;
  929. set_linear_ip(&regs, ibs_data.regs[1]);
  930. regs.flags |= PERF_EFLAGS_EXACT;
  931. }
  932. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  933. raw = (struct perf_raw_record){
  934. .frag = {
  935. .size = sizeof(u32) + ibs_data.size,
  936. .data = ibs_data.data,
  937. },
  938. };
  939. data.raw = &raw;
  940. data.sample_flags |= PERF_SAMPLE_RAW;
  941. }
  942. if (perf_ibs == &perf_ibs_op)
  943. perf_ibs_parse_ld_st_data(event->attr.sample_type, &ibs_data, &data);
  944. /*
  945. * rip recorded by IbsOpRip will not be consistent with rsp and rbp
  946. * recorded as part of interrupt regs. Thus we need to use rip from
  947. * interrupt regs while unwinding call stack.
  948. */
  949. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  950. data.callchain = perf_callchain(event, iregs);
  951. data.sample_flags |= PERF_SAMPLE_CALLCHAIN;
  952. }
  953. throttle = perf_event_overflow(event, &data, &regs);
  954. out:
  955. if (throttle) {
  956. perf_ibs_stop(event, 0);
  957. } else {
  958. if (perf_ibs == &perf_ibs_op) {
  959. if (ibs_caps & IBS_CAPS_OPCNTEXT) {
  960. new_config = period & IBS_OP_MAX_CNT_EXT_MASK;
  961. period &= ~IBS_OP_MAX_CNT_EXT_MASK;
  962. }
  963. if ((ibs_caps & IBS_CAPS_RDWROPCNT) && (*config & IBS_OP_CNT_CTL))
  964. new_config |= *config & IBS_OP_CUR_CNT_RAND;
  965. }
  966. new_config |= period >> 4;
  967. perf_ibs_enable_event(perf_ibs, hwc, new_config);
  968. }
  969. perf_event_update_userpage(event);
  970. return 1;
  971. }
  972. static int
  973. perf_ibs_nmi_handler(unsigned int cmd, struct pt_regs *regs)
  974. {
  975. u64 stamp = sched_clock();
  976. int handled = 0;
  977. handled += perf_ibs_handle_irq(&perf_ibs_fetch, regs);
  978. handled += perf_ibs_handle_irq(&perf_ibs_op, regs);
  979. if (handled)
  980. inc_irq_stat(apic_perf_irqs);
  981. perf_sample_event_took(sched_clock() - stamp);
  982. return handled;
  983. }
  984. NOKPROBE_SYMBOL(perf_ibs_nmi_handler);
  985. static __init int perf_ibs_pmu_init(struct perf_ibs *perf_ibs, char *name)
  986. {
  987. struct cpu_perf_ibs __percpu *pcpu;
  988. int ret;
  989. pcpu = alloc_percpu(struct cpu_perf_ibs);
  990. if (!pcpu)
  991. return -ENOMEM;
  992. perf_ibs->pcpu = pcpu;
  993. ret = perf_pmu_register(&perf_ibs->pmu, name, -1);
  994. if (ret) {
  995. perf_ibs->pcpu = NULL;
  996. free_percpu(pcpu);
  997. }
  998. return ret;
  999. }
  1000. static __init int perf_ibs_fetch_init(void)
  1001. {
  1002. /*
  1003. * Some chips fail to reset the fetch count when it is written; instead
  1004. * they need a 0-1 transition of IbsFetchEn.
  1005. */
  1006. if (boot_cpu_data.x86 >= 0x16 && boot_cpu_data.x86 <= 0x18)
  1007. perf_ibs_fetch.fetch_count_reset_broken = 1;
  1008. if (boot_cpu_data.x86 == 0x19 && boot_cpu_data.x86_model < 0x10)
  1009. perf_ibs_fetch.fetch_ignore_if_zero_rip = 1;
  1010. if (ibs_caps & IBS_CAPS_ZEN4)
  1011. perf_ibs_fetch.config_mask |= IBS_FETCH_L3MISSONLY;
  1012. perf_ibs_fetch.pmu.attr_groups = fetch_attr_groups;
  1013. perf_ibs_fetch.pmu.attr_update = fetch_attr_update;
  1014. return perf_ibs_pmu_init(&perf_ibs_fetch, "ibs_fetch");
  1015. }
  1016. static __init int perf_ibs_op_init(void)
  1017. {
  1018. if (ibs_caps & IBS_CAPS_OPCNT)
  1019. perf_ibs_op.config_mask |= IBS_OP_CNT_CTL;
  1020. if (ibs_caps & IBS_CAPS_OPCNTEXT) {
  1021. perf_ibs_op.max_period |= IBS_OP_MAX_CNT_EXT_MASK;
  1022. perf_ibs_op.config_mask |= IBS_OP_MAX_CNT_EXT_MASK;
  1023. perf_ibs_op.cnt_mask |= IBS_OP_MAX_CNT_EXT_MASK;
  1024. }
  1025. if (ibs_caps & IBS_CAPS_ZEN4)
  1026. perf_ibs_op.config_mask |= IBS_OP_L3MISSONLY;
  1027. perf_ibs_op.pmu.attr_groups = empty_attr_groups;
  1028. perf_ibs_op.pmu.attr_update = op_attr_update;
  1029. return perf_ibs_pmu_init(&perf_ibs_op, "ibs_op");
  1030. }
  1031. static __init int perf_event_ibs_init(void)
  1032. {
  1033. int ret;
  1034. ret = perf_ibs_fetch_init();
  1035. if (ret)
  1036. return ret;
  1037. ret = perf_ibs_op_init();
  1038. if (ret)
  1039. goto err_op;
  1040. ret = register_nmi_handler(NMI_LOCAL, perf_ibs_nmi_handler, 0, "perf_ibs");
  1041. if (ret)
  1042. goto err_nmi;
  1043. pr_info("perf: AMD IBS detected (0x%08x)\n", ibs_caps);
  1044. return 0;
  1045. err_nmi:
  1046. perf_pmu_unregister(&perf_ibs_op.pmu);
  1047. free_percpu(perf_ibs_op.pcpu);
  1048. perf_ibs_op.pcpu = NULL;
  1049. err_op:
  1050. perf_pmu_unregister(&perf_ibs_fetch.pmu);
  1051. free_percpu(perf_ibs_fetch.pcpu);
  1052. perf_ibs_fetch.pcpu = NULL;
  1053. return ret;
  1054. }
  1055. #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
  1056. static __init int perf_event_ibs_init(void)
  1057. {
  1058. return 0;
  1059. }
  1060. #endif
  1061. /* IBS - apic initialization, for perf and oprofile */
  1062. static __init u32 __get_ibs_caps(void)
  1063. {
  1064. u32 caps;
  1065. unsigned int max_level;
  1066. if (!boot_cpu_has(X86_FEATURE_IBS))
  1067. return 0;
  1068. /* check IBS cpuid feature flags */
  1069. max_level = cpuid_eax(0x80000000);
  1070. if (max_level < IBS_CPUID_FEATURES)
  1071. return IBS_CAPS_DEFAULT;
  1072. caps = cpuid_eax(IBS_CPUID_FEATURES);
  1073. if (!(caps & IBS_CAPS_AVAIL))
  1074. /* cpuid flags not valid */
  1075. return IBS_CAPS_DEFAULT;
  1076. return caps;
  1077. }
  1078. u32 get_ibs_caps(void)
  1079. {
  1080. return ibs_caps;
  1081. }
  1082. EXPORT_SYMBOL(get_ibs_caps);
  1083. static inline int get_eilvt(int offset)
  1084. {
  1085. return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
  1086. }
  1087. static inline int put_eilvt(int offset)
  1088. {
  1089. return !setup_APIC_eilvt(offset, 0, 0, 1);
  1090. }
  1091. /*
  1092. * Check and reserve APIC extended interrupt LVT offset for IBS if available.
  1093. */
  1094. static inline int ibs_eilvt_valid(void)
  1095. {
  1096. int offset;
  1097. u64 val;
  1098. int valid = 0;
  1099. preempt_disable();
  1100. rdmsrl(MSR_AMD64_IBSCTL, val);
  1101. offset = val & IBSCTL_LVT_OFFSET_MASK;
  1102. if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
  1103. pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
  1104. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  1105. goto out;
  1106. }
  1107. if (!get_eilvt(offset)) {
  1108. pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
  1109. smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
  1110. goto out;
  1111. }
  1112. valid = 1;
  1113. out:
  1114. preempt_enable();
  1115. return valid;
  1116. }
  1117. static int setup_ibs_ctl(int ibs_eilvt_off)
  1118. {
  1119. struct pci_dev *cpu_cfg;
  1120. int nodes;
  1121. u32 value = 0;
  1122. nodes = 0;
  1123. cpu_cfg = NULL;
  1124. do {
  1125. cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
  1126. PCI_DEVICE_ID_AMD_10H_NB_MISC,
  1127. cpu_cfg);
  1128. if (!cpu_cfg)
  1129. break;
  1130. ++nodes;
  1131. pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
  1132. | IBSCTL_LVT_OFFSET_VALID);
  1133. pci_read_config_dword(cpu_cfg, IBSCTL, &value);
  1134. if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
  1135. pci_dev_put(cpu_cfg);
  1136. pr_debug("Failed to setup IBS LVT offset, IBSCTL = 0x%08x\n",
  1137. value);
  1138. return -EINVAL;
  1139. }
  1140. } while (1);
  1141. if (!nodes) {
  1142. pr_debug("No CPU node configured for IBS\n");
  1143. return -ENODEV;
  1144. }
  1145. return 0;
  1146. }
  1147. /*
  1148. * This runs only on the current cpu. We try to find an LVT offset and
  1149. * setup the local APIC. For this we must disable preemption. On
  1150. * success we initialize all nodes with this offset. This updates then
  1151. * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
  1152. * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
  1153. * is using the new offset.
  1154. */
  1155. static void force_ibs_eilvt_setup(void)
  1156. {
  1157. int offset;
  1158. int ret;
  1159. preempt_disable();
  1160. /* find the next free available EILVT entry, skip offset 0 */
  1161. for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
  1162. if (get_eilvt(offset))
  1163. break;
  1164. }
  1165. preempt_enable();
  1166. if (offset == APIC_EILVT_NR_MAX) {
  1167. pr_debug("No EILVT entry available\n");
  1168. return;
  1169. }
  1170. ret = setup_ibs_ctl(offset);
  1171. if (ret)
  1172. goto out;
  1173. if (!ibs_eilvt_valid())
  1174. goto out;
  1175. pr_info("LVT offset %d assigned\n", offset);
  1176. return;
  1177. out:
  1178. preempt_disable();
  1179. put_eilvt(offset);
  1180. preempt_enable();
  1181. return;
  1182. }
  1183. static void ibs_eilvt_setup(void)
  1184. {
  1185. /*
  1186. * Force LVT offset assignment for family 10h: The offsets are
  1187. * not assigned by the BIOS for this family, so the OS is
  1188. * responsible for doing it. If the OS assignment fails, fall
  1189. * back to BIOS settings and try to setup this.
  1190. */
  1191. if (boot_cpu_data.x86 == 0x10)
  1192. force_ibs_eilvt_setup();
  1193. }
  1194. static inline int get_ibs_lvt_offset(void)
  1195. {
  1196. u64 val;
  1197. rdmsrl(MSR_AMD64_IBSCTL, val);
  1198. if (!(val & IBSCTL_LVT_OFFSET_VALID))
  1199. return -EINVAL;
  1200. return val & IBSCTL_LVT_OFFSET_MASK;
  1201. }
  1202. static void setup_APIC_ibs(void)
  1203. {
  1204. int offset;
  1205. offset = get_ibs_lvt_offset();
  1206. if (offset < 0)
  1207. goto failed;
  1208. if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
  1209. return;
  1210. failed:
  1211. pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
  1212. smp_processor_id());
  1213. }
  1214. static void clear_APIC_ibs(void)
  1215. {
  1216. int offset;
  1217. offset = get_ibs_lvt_offset();
  1218. if (offset >= 0)
  1219. setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
  1220. }
  1221. static int x86_pmu_amd_ibs_starting_cpu(unsigned int cpu)
  1222. {
  1223. setup_APIC_ibs();
  1224. return 0;
  1225. }
  1226. #ifdef CONFIG_PM
  1227. static int perf_ibs_suspend(void)
  1228. {
  1229. clear_APIC_ibs();
  1230. return 0;
  1231. }
  1232. static void perf_ibs_resume(void)
  1233. {
  1234. ibs_eilvt_setup();
  1235. setup_APIC_ibs();
  1236. }
  1237. static struct syscore_ops perf_ibs_syscore_ops = {
  1238. .resume = perf_ibs_resume,
  1239. .suspend = perf_ibs_suspend,
  1240. };
  1241. static void perf_ibs_pm_init(void)
  1242. {
  1243. register_syscore_ops(&perf_ibs_syscore_ops);
  1244. }
  1245. #else
  1246. static inline void perf_ibs_pm_init(void) { }
  1247. #endif
  1248. static int x86_pmu_amd_ibs_dying_cpu(unsigned int cpu)
  1249. {
  1250. clear_APIC_ibs();
  1251. return 0;
  1252. }
  1253. static __init int amd_ibs_init(void)
  1254. {
  1255. u32 caps;
  1256. caps = __get_ibs_caps();
  1257. if (!caps)
  1258. return -ENODEV; /* ibs not supported by the cpu */
  1259. ibs_eilvt_setup();
  1260. if (!ibs_eilvt_valid())
  1261. return -EINVAL;
  1262. perf_ibs_pm_init();
  1263. ibs_caps = caps;
  1264. /* make ibs_caps visible to other cpus: */
  1265. smp_mb();
  1266. /*
  1267. * x86_pmu_amd_ibs_starting_cpu will be called from core on
  1268. * all online cpus.
  1269. */
  1270. cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_IBS_STARTING,
  1271. "perf/x86/amd/ibs:starting",
  1272. x86_pmu_amd_ibs_starting_cpu,
  1273. x86_pmu_amd_ibs_dying_cpu);
  1274. return perf_event_ibs_init();
  1275. }
  1276. /* Since we need the pci subsystem to init ibs we can't do this earlier: */
  1277. device_initcall(amd_ibs_init);