arm_spe_pmu.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Perf support for the Statistical Profiling Extension, introduced as
  4. * part of ARMv8.2.
  5. *
  6. * Copyright (C) 2016 ARM Limited
  7. *
  8. * Author: Will Deacon <will.deacon@arm.com>
  9. */
  10. #define PMUNAME "arm_spe"
  11. #define DRVNAME PMUNAME "_pmu"
  12. #define pr_fmt(fmt) DRVNAME ": " fmt
  13. #include <linux/bitfield.h>
  14. #include <linux/bitops.h>
  15. #include <linux/bug.h>
  16. #include <linux/capability.h>
  17. #include <linux/cpuhotplug.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/device.h>
  20. #include <linux/errno.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/kernel.h>
  24. #include <linux/list.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/perf_event.h>
  28. #include <linux/perf/arm_pmu.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/printk.h>
  31. #include <linux/slab.h>
  32. #include <linux/smp.h>
  33. #include <linux/vmalloc.h>
  34. #include <asm/barrier.h>
  35. #include <asm/cpufeature.h>
  36. #include <asm/mmu.h>
  37. #include <asm/sysreg.h>
  38. /*
  39. * Cache if the event is allowed to trace Context information.
  40. * This allows us to perform the check, i.e, perf_allow_kernel(),
  41. * in the context of the event owner, once, during the event_init().
  42. */
  43. #define SPE_PMU_HW_FLAGS_CX 0x00001
  44. static_assert((PERF_EVENT_FLAG_ARCH & SPE_PMU_HW_FLAGS_CX) == SPE_PMU_HW_FLAGS_CX);
  45. static void set_spe_event_has_cx(struct perf_event *event)
  46. {
  47. if (IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR) && !perf_allow_kernel(&event->attr))
  48. event->hw.flags |= SPE_PMU_HW_FLAGS_CX;
  49. }
  50. static bool get_spe_event_has_cx(struct perf_event *event)
  51. {
  52. return !!(event->hw.flags & SPE_PMU_HW_FLAGS_CX);
  53. }
  54. #define ARM_SPE_BUF_PAD_BYTE 0
  55. struct arm_spe_pmu_buf {
  56. int nr_pages;
  57. bool snapshot;
  58. void *base;
  59. };
  60. struct arm_spe_pmu {
  61. struct pmu pmu;
  62. struct platform_device *pdev;
  63. cpumask_t supported_cpus;
  64. struct hlist_node hotplug_node;
  65. int irq; /* PPI */
  66. u16 pmsver;
  67. u16 min_period;
  68. u16 counter_sz;
  69. #define SPE_PMU_FEAT_FILT_EVT (1UL << 0)
  70. #define SPE_PMU_FEAT_FILT_TYP (1UL << 1)
  71. #define SPE_PMU_FEAT_FILT_LAT (1UL << 2)
  72. #define SPE_PMU_FEAT_ARCH_INST (1UL << 3)
  73. #define SPE_PMU_FEAT_LDS (1UL << 4)
  74. #define SPE_PMU_FEAT_ERND (1UL << 5)
  75. #define SPE_PMU_FEAT_INV_FILT_EVT (1UL << 6)
  76. #define SPE_PMU_FEAT_DEV_PROBED (1UL << 63)
  77. u64 features;
  78. u16 max_record_sz;
  79. u16 align;
  80. struct perf_output_handle __percpu *handle;
  81. };
  82. #define to_spe_pmu(p) (container_of(p, struct arm_spe_pmu, pmu))
  83. /* Convert a free-running index from perf into an SPE buffer offset */
  84. #define PERF_IDX2OFF(idx, buf) \
  85. ((idx) % ((unsigned long)(buf)->nr_pages << PAGE_SHIFT))
  86. /* Keep track of our dynamic hotplug state */
  87. static enum cpuhp_state arm_spe_pmu_online;
  88. enum arm_spe_pmu_buf_fault_action {
  89. SPE_PMU_BUF_FAULT_ACT_SPURIOUS,
  90. SPE_PMU_BUF_FAULT_ACT_FATAL,
  91. SPE_PMU_BUF_FAULT_ACT_OK,
  92. };
  93. /* This sysfs gunk was really good fun to write. */
  94. enum arm_spe_pmu_capabilities {
  95. SPE_PMU_CAP_ARCH_INST = 0,
  96. SPE_PMU_CAP_ERND,
  97. SPE_PMU_CAP_FEAT_MAX,
  98. SPE_PMU_CAP_CNT_SZ = SPE_PMU_CAP_FEAT_MAX,
  99. SPE_PMU_CAP_MIN_IVAL,
  100. };
  101. static int arm_spe_pmu_feat_caps[SPE_PMU_CAP_FEAT_MAX] = {
  102. [SPE_PMU_CAP_ARCH_INST] = SPE_PMU_FEAT_ARCH_INST,
  103. [SPE_PMU_CAP_ERND] = SPE_PMU_FEAT_ERND,
  104. };
  105. static u32 arm_spe_pmu_cap_get(struct arm_spe_pmu *spe_pmu, int cap)
  106. {
  107. if (cap < SPE_PMU_CAP_FEAT_MAX)
  108. return !!(spe_pmu->features & arm_spe_pmu_feat_caps[cap]);
  109. switch (cap) {
  110. case SPE_PMU_CAP_CNT_SZ:
  111. return spe_pmu->counter_sz;
  112. case SPE_PMU_CAP_MIN_IVAL:
  113. return spe_pmu->min_period;
  114. default:
  115. WARN(1, "unknown cap %d\n", cap);
  116. }
  117. return 0;
  118. }
  119. static ssize_t arm_spe_pmu_cap_show(struct device *dev,
  120. struct device_attribute *attr,
  121. char *buf)
  122. {
  123. struct arm_spe_pmu *spe_pmu = dev_get_drvdata(dev);
  124. struct dev_ext_attribute *ea =
  125. container_of(attr, struct dev_ext_attribute, attr);
  126. int cap = (long)ea->var;
  127. return sysfs_emit(buf, "%u\n", arm_spe_pmu_cap_get(spe_pmu, cap));
  128. }
  129. #define SPE_EXT_ATTR_ENTRY(_name, _func, _var) \
  130. &((struct dev_ext_attribute[]) { \
  131. { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_var } \
  132. })[0].attr.attr
  133. #define SPE_CAP_EXT_ATTR_ENTRY(_name, _var) \
  134. SPE_EXT_ATTR_ENTRY(_name, arm_spe_pmu_cap_show, _var)
  135. static struct attribute *arm_spe_pmu_cap_attr[] = {
  136. SPE_CAP_EXT_ATTR_ENTRY(arch_inst, SPE_PMU_CAP_ARCH_INST),
  137. SPE_CAP_EXT_ATTR_ENTRY(ernd, SPE_PMU_CAP_ERND),
  138. SPE_CAP_EXT_ATTR_ENTRY(count_size, SPE_PMU_CAP_CNT_SZ),
  139. SPE_CAP_EXT_ATTR_ENTRY(min_interval, SPE_PMU_CAP_MIN_IVAL),
  140. NULL,
  141. };
  142. static const struct attribute_group arm_spe_pmu_cap_group = {
  143. .name = "caps",
  144. .attrs = arm_spe_pmu_cap_attr,
  145. };
  146. /* User ABI */
  147. #define ATTR_CFG_FLD_ts_enable_CFG config /* PMSCR_EL1.TS */
  148. #define ATTR_CFG_FLD_ts_enable_LO 0
  149. #define ATTR_CFG_FLD_ts_enable_HI 0
  150. #define ATTR_CFG_FLD_pa_enable_CFG config /* PMSCR_EL1.PA */
  151. #define ATTR_CFG_FLD_pa_enable_LO 1
  152. #define ATTR_CFG_FLD_pa_enable_HI 1
  153. #define ATTR_CFG_FLD_pct_enable_CFG config /* PMSCR_EL1.PCT */
  154. #define ATTR_CFG_FLD_pct_enable_LO 2
  155. #define ATTR_CFG_FLD_pct_enable_HI 2
  156. #define ATTR_CFG_FLD_jitter_CFG config /* PMSIRR_EL1.RND */
  157. #define ATTR_CFG_FLD_jitter_LO 16
  158. #define ATTR_CFG_FLD_jitter_HI 16
  159. #define ATTR_CFG_FLD_branch_filter_CFG config /* PMSFCR_EL1.B */
  160. #define ATTR_CFG_FLD_branch_filter_LO 32
  161. #define ATTR_CFG_FLD_branch_filter_HI 32
  162. #define ATTR_CFG_FLD_load_filter_CFG config /* PMSFCR_EL1.LD */
  163. #define ATTR_CFG_FLD_load_filter_LO 33
  164. #define ATTR_CFG_FLD_load_filter_HI 33
  165. #define ATTR_CFG_FLD_store_filter_CFG config /* PMSFCR_EL1.ST */
  166. #define ATTR_CFG_FLD_store_filter_LO 34
  167. #define ATTR_CFG_FLD_store_filter_HI 34
  168. #define ATTR_CFG_FLD_event_filter_CFG config1 /* PMSEVFR_EL1 */
  169. #define ATTR_CFG_FLD_event_filter_LO 0
  170. #define ATTR_CFG_FLD_event_filter_HI 63
  171. #define ATTR_CFG_FLD_min_latency_CFG config2 /* PMSLATFR_EL1.MINLAT */
  172. #define ATTR_CFG_FLD_min_latency_LO 0
  173. #define ATTR_CFG_FLD_min_latency_HI 11
  174. #define ATTR_CFG_FLD_inv_event_filter_CFG config3 /* PMSNEVFR_EL1 */
  175. #define ATTR_CFG_FLD_inv_event_filter_LO 0
  176. #define ATTR_CFG_FLD_inv_event_filter_HI 63
  177. GEN_PMU_FORMAT_ATTR(ts_enable);
  178. GEN_PMU_FORMAT_ATTR(pa_enable);
  179. GEN_PMU_FORMAT_ATTR(pct_enable);
  180. GEN_PMU_FORMAT_ATTR(jitter);
  181. GEN_PMU_FORMAT_ATTR(branch_filter);
  182. GEN_PMU_FORMAT_ATTR(load_filter);
  183. GEN_PMU_FORMAT_ATTR(store_filter);
  184. GEN_PMU_FORMAT_ATTR(event_filter);
  185. GEN_PMU_FORMAT_ATTR(inv_event_filter);
  186. GEN_PMU_FORMAT_ATTR(min_latency);
  187. static struct attribute *arm_spe_pmu_formats_attr[] = {
  188. &format_attr_ts_enable.attr,
  189. &format_attr_pa_enable.attr,
  190. &format_attr_pct_enable.attr,
  191. &format_attr_jitter.attr,
  192. &format_attr_branch_filter.attr,
  193. &format_attr_load_filter.attr,
  194. &format_attr_store_filter.attr,
  195. &format_attr_event_filter.attr,
  196. &format_attr_inv_event_filter.attr,
  197. &format_attr_min_latency.attr,
  198. NULL,
  199. };
  200. static umode_t arm_spe_pmu_format_attr_is_visible(struct kobject *kobj,
  201. struct attribute *attr,
  202. int unused)
  203. {
  204. struct device *dev = kobj_to_dev(kobj);
  205. struct arm_spe_pmu *spe_pmu = dev_get_drvdata(dev);
  206. if (attr == &format_attr_inv_event_filter.attr && !(spe_pmu->features & SPE_PMU_FEAT_INV_FILT_EVT))
  207. return 0;
  208. return attr->mode;
  209. }
  210. static const struct attribute_group arm_spe_pmu_format_group = {
  211. .name = "format",
  212. .is_visible = arm_spe_pmu_format_attr_is_visible,
  213. .attrs = arm_spe_pmu_formats_attr,
  214. };
  215. static ssize_t cpumask_show(struct device *dev,
  216. struct device_attribute *attr, char *buf)
  217. {
  218. struct arm_spe_pmu *spe_pmu = dev_get_drvdata(dev);
  219. return cpumap_print_to_pagebuf(true, buf, &spe_pmu->supported_cpus);
  220. }
  221. static DEVICE_ATTR_RO(cpumask);
  222. static struct attribute *arm_spe_pmu_attrs[] = {
  223. &dev_attr_cpumask.attr,
  224. NULL,
  225. };
  226. static const struct attribute_group arm_spe_pmu_group = {
  227. .attrs = arm_spe_pmu_attrs,
  228. };
  229. static const struct attribute_group *arm_spe_pmu_attr_groups[] = {
  230. &arm_spe_pmu_group,
  231. &arm_spe_pmu_cap_group,
  232. &arm_spe_pmu_format_group,
  233. NULL,
  234. };
  235. /* Convert between user ABI and register values */
  236. static u64 arm_spe_event_to_pmscr(struct perf_event *event)
  237. {
  238. struct perf_event_attr *attr = &event->attr;
  239. u64 reg = 0;
  240. reg |= FIELD_PREP(PMSCR_EL1_TS, ATTR_CFG_GET_FLD(attr, ts_enable));
  241. reg |= FIELD_PREP(PMSCR_EL1_PA, ATTR_CFG_GET_FLD(attr, pa_enable));
  242. reg |= FIELD_PREP(PMSCR_EL1_PCT, ATTR_CFG_GET_FLD(attr, pct_enable));
  243. if (!attr->exclude_user)
  244. reg |= PMSCR_EL1_E0SPE;
  245. if (!attr->exclude_kernel)
  246. reg |= PMSCR_EL1_E1SPE;
  247. if (get_spe_event_has_cx(event))
  248. reg |= PMSCR_EL1_CX;
  249. return reg;
  250. }
  251. static void arm_spe_event_sanitise_period(struct perf_event *event)
  252. {
  253. struct arm_spe_pmu *spe_pmu = to_spe_pmu(event->pmu);
  254. u64 period = event->hw.sample_period;
  255. u64 max_period = PMSIRR_EL1_INTERVAL_MASK;
  256. if (period < spe_pmu->min_period)
  257. period = spe_pmu->min_period;
  258. else if (period > max_period)
  259. period = max_period;
  260. else
  261. period &= max_period;
  262. event->hw.sample_period = period;
  263. }
  264. static u64 arm_spe_event_to_pmsirr(struct perf_event *event)
  265. {
  266. struct perf_event_attr *attr = &event->attr;
  267. u64 reg = 0;
  268. arm_spe_event_sanitise_period(event);
  269. reg |= FIELD_PREP(PMSIRR_EL1_RND, ATTR_CFG_GET_FLD(attr, jitter));
  270. reg |= event->hw.sample_period;
  271. return reg;
  272. }
  273. static u64 arm_spe_event_to_pmsfcr(struct perf_event *event)
  274. {
  275. struct perf_event_attr *attr = &event->attr;
  276. u64 reg = 0;
  277. reg |= FIELD_PREP(PMSFCR_EL1_LD, ATTR_CFG_GET_FLD(attr, load_filter));
  278. reg |= FIELD_PREP(PMSFCR_EL1_ST, ATTR_CFG_GET_FLD(attr, store_filter));
  279. reg |= FIELD_PREP(PMSFCR_EL1_B, ATTR_CFG_GET_FLD(attr, branch_filter));
  280. if (reg)
  281. reg |= PMSFCR_EL1_FT;
  282. if (ATTR_CFG_GET_FLD(attr, event_filter))
  283. reg |= PMSFCR_EL1_FE;
  284. if (ATTR_CFG_GET_FLD(attr, inv_event_filter))
  285. reg |= PMSFCR_EL1_FnE;
  286. if (ATTR_CFG_GET_FLD(attr, min_latency))
  287. reg |= PMSFCR_EL1_FL;
  288. return reg;
  289. }
  290. static u64 arm_spe_event_to_pmsevfr(struct perf_event *event)
  291. {
  292. struct perf_event_attr *attr = &event->attr;
  293. return ATTR_CFG_GET_FLD(attr, event_filter);
  294. }
  295. static u64 arm_spe_event_to_pmsnevfr(struct perf_event *event)
  296. {
  297. struct perf_event_attr *attr = &event->attr;
  298. return ATTR_CFG_GET_FLD(attr, inv_event_filter);
  299. }
  300. static u64 arm_spe_event_to_pmslatfr(struct perf_event *event)
  301. {
  302. struct perf_event_attr *attr = &event->attr;
  303. return FIELD_PREP(PMSLATFR_EL1_MINLAT, ATTR_CFG_GET_FLD(attr, min_latency));
  304. }
  305. static void arm_spe_pmu_pad_buf(struct perf_output_handle *handle, int len)
  306. {
  307. struct arm_spe_pmu_buf *buf = perf_get_aux(handle);
  308. u64 head = PERF_IDX2OFF(handle->head, buf);
  309. memset(buf->base + head, ARM_SPE_BUF_PAD_BYTE, len);
  310. if (!buf->snapshot)
  311. perf_aux_output_skip(handle, len);
  312. }
  313. static u64 arm_spe_pmu_next_snapshot_off(struct perf_output_handle *handle)
  314. {
  315. struct arm_spe_pmu_buf *buf = perf_get_aux(handle);
  316. struct arm_spe_pmu *spe_pmu = to_spe_pmu(handle->event->pmu);
  317. u64 head = PERF_IDX2OFF(handle->head, buf);
  318. u64 limit = buf->nr_pages * PAGE_SIZE;
  319. /*
  320. * The trace format isn't parseable in reverse, so clamp
  321. * the limit to half of the buffer size in snapshot mode
  322. * so that the worst case is half a buffer of records, as
  323. * opposed to a single record.
  324. */
  325. if (head < limit >> 1)
  326. limit >>= 1;
  327. /*
  328. * If we're within max_record_sz of the limit, we must
  329. * pad, move the head index and recompute the limit.
  330. */
  331. if (limit - head < spe_pmu->max_record_sz) {
  332. arm_spe_pmu_pad_buf(handle, limit - head);
  333. handle->head = PERF_IDX2OFF(limit, buf);
  334. limit = ((buf->nr_pages * PAGE_SIZE) >> 1) + handle->head;
  335. }
  336. return limit;
  337. }
  338. static u64 __arm_spe_pmu_next_off(struct perf_output_handle *handle)
  339. {
  340. struct arm_spe_pmu *spe_pmu = to_spe_pmu(handle->event->pmu);
  341. struct arm_spe_pmu_buf *buf = perf_get_aux(handle);
  342. const u64 bufsize = buf->nr_pages * PAGE_SIZE;
  343. u64 limit = bufsize;
  344. u64 head, tail, wakeup;
  345. /*
  346. * The head can be misaligned for two reasons:
  347. *
  348. * 1. The hardware left PMBPTR pointing to the first byte after
  349. * a record when generating a buffer management event.
  350. *
  351. * 2. We used perf_aux_output_skip to consume handle->size bytes
  352. * and CIRC_SPACE was used to compute the size, which always
  353. * leaves one entry free.
  354. *
  355. * Deal with this by padding to the next alignment boundary and
  356. * moving the head index. If we run out of buffer space, we'll
  357. * reduce handle->size to zero and end up reporting truncation.
  358. */
  359. head = PERF_IDX2OFF(handle->head, buf);
  360. if (!IS_ALIGNED(head, spe_pmu->align)) {
  361. unsigned long delta = roundup(head, spe_pmu->align) - head;
  362. delta = min(delta, handle->size);
  363. arm_spe_pmu_pad_buf(handle, delta);
  364. head = PERF_IDX2OFF(handle->head, buf);
  365. }
  366. /* If we've run out of free space, then nothing more to do */
  367. if (!handle->size)
  368. goto no_space;
  369. /* Compute the tail and wakeup indices now that we've aligned head */
  370. tail = PERF_IDX2OFF(handle->head + handle->size, buf);
  371. wakeup = PERF_IDX2OFF(handle->wakeup, buf);
  372. /*
  373. * Avoid clobbering unconsumed data. We know we have space, so
  374. * if we see head == tail we know that the buffer is empty. If
  375. * head > tail, then there's nothing to clobber prior to
  376. * wrapping.
  377. */
  378. if (head < tail)
  379. limit = round_down(tail, PAGE_SIZE);
  380. /*
  381. * Wakeup may be arbitrarily far into the future. If it's not in
  382. * the current generation, either we'll wrap before hitting it,
  383. * or it's in the past and has been handled already.
  384. *
  385. * If there's a wakeup before we wrap, arrange to be woken up by
  386. * the page boundary following it. Keep the tail boundary if
  387. * that's lower.
  388. */
  389. if (handle->wakeup < (handle->head + handle->size) && head <= wakeup)
  390. limit = min(limit, round_up(wakeup, PAGE_SIZE));
  391. if (limit > head)
  392. return limit;
  393. arm_spe_pmu_pad_buf(handle, handle->size);
  394. no_space:
  395. perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
  396. perf_aux_output_end(handle, 0);
  397. return 0;
  398. }
  399. static u64 arm_spe_pmu_next_off(struct perf_output_handle *handle)
  400. {
  401. struct arm_spe_pmu_buf *buf = perf_get_aux(handle);
  402. struct arm_spe_pmu *spe_pmu = to_spe_pmu(handle->event->pmu);
  403. u64 limit = __arm_spe_pmu_next_off(handle);
  404. u64 head = PERF_IDX2OFF(handle->head, buf);
  405. /*
  406. * If the head has come too close to the end of the buffer,
  407. * then pad to the end and recompute the limit.
  408. */
  409. if (limit && (limit - head < spe_pmu->max_record_sz)) {
  410. arm_spe_pmu_pad_buf(handle, limit - head);
  411. limit = __arm_spe_pmu_next_off(handle);
  412. }
  413. return limit;
  414. }
  415. static void arm_spe_perf_aux_output_begin(struct perf_output_handle *handle,
  416. struct perf_event *event)
  417. {
  418. u64 base, limit;
  419. struct arm_spe_pmu_buf *buf;
  420. /* Start a new aux session */
  421. buf = perf_aux_output_begin(handle, event);
  422. if (!buf) {
  423. event->hw.state |= PERF_HES_STOPPED;
  424. /*
  425. * We still need to clear the limit pointer, since the
  426. * profiler might only be disabled by virtue of a fault.
  427. */
  428. limit = 0;
  429. goto out_write_limit;
  430. }
  431. limit = buf->snapshot ? arm_spe_pmu_next_snapshot_off(handle)
  432. : arm_spe_pmu_next_off(handle);
  433. if (limit)
  434. limit |= PMBLIMITR_EL1_E;
  435. limit += (u64)buf->base;
  436. base = (u64)buf->base + PERF_IDX2OFF(handle->head, buf);
  437. write_sysreg_s(base, SYS_PMBPTR_EL1);
  438. out_write_limit:
  439. write_sysreg_s(limit, SYS_PMBLIMITR_EL1);
  440. }
  441. static void arm_spe_perf_aux_output_end(struct perf_output_handle *handle)
  442. {
  443. struct arm_spe_pmu_buf *buf = perf_get_aux(handle);
  444. u64 offset, size;
  445. offset = read_sysreg_s(SYS_PMBPTR_EL1) - (u64)buf->base;
  446. size = offset - PERF_IDX2OFF(handle->head, buf);
  447. if (buf->snapshot)
  448. handle->head = offset;
  449. perf_aux_output_end(handle, size);
  450. }
  451. static void arm_spe_pmu_disable_and_drain_local(void)
  452. {
  453. /* Disable profiling at EL0 and EL1 */
  454. write_sysreg_s(0, SYS_PMSCR_EL1);
  455. isb();
  456. /* Drain any buffered data */
  457. psb_csync();
  458. dsb(nsh);
  459. /* Disable the profiling buffer */
  460. write_sysreg_s(0, SYS_PMBLIMITR_EL1);
  461. isb();
  462. }
  463. /* IRQ handling */
  464. static enum arm_spe_pmu_buf_fault_action
  465. arm_spe_pmu_buf_get_fault_act(struct perf_output_handle *handle)
  466. {
  467. const char *err_str;
  468. u64 pmbsr;
  469. enum arm_spe_pmu_buf_fault_action ret;
  470. /*
  471. * Ensure new profiling data is visible to the CPU and any external
  472. * aborts have been resolved.
  473. */
  474. psb_csync();
  475. dsb(nsh);
  476. /* Ensure hardware updates to PMBPTR_EL1 are visible */
  477. isb();
  478. /* Service required? */
  479. pmbsr = read_sysreg_s(SYS_PMBSR_EL1);
  480. if (!FIELD_GET(PMBSR_EL1_S, pmbsr))
  481. return SPE_PMU_BUF_FAULT_ACT_SPURIOUS;
  482. /*
  483. * If we've lost data, disable profiling and also set the PARTIAL
  484. * flag to indicate that the last record is corrupted.
  485. */
  486. if (FIELD_GET(PMBSR_EL1_DL, pmbsr))
  487. perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED |
  488. PERF_AUX_FLAG_PARTIAL);
  489. /* Report collisions to userspace so that it can up the period */
  490. if (FIELD_GET(PMBSR_EL1_COLL, pmbsr))
  491. perf_aux_output_flag(handle, PERF_AUX_FLAG_COLLISION);
  492. /* We only expect buffer management events */
  493. switch (FIELD_GET(PMBSR_EL1_EC, pmbsr)) {
  494. case PMBSR_EL1_EC_BUF:
  495. /* Handled below */
  496. break;
  497. case PMBSR_EL1_EC_FAULT_S1:
  498. case PMBSR_EL1_EC_FAULT_S2:
  499. err_str = "Unexpected buffer fault";
  500. goto out_err;
  501. default:
  502. err_str = "Unknown error code";
  503. goto out_err;
  504. }
  505. /* Buffer management event */
  506. switch (FIELD_GET(PMBSR_EL1_BUF_BSC_MASK, pmbsr)) {
  507. case PMBSR_EL1_BUF_BSC_FULL:
  508. ret = SPE_PMU_BUF_FAULT_ACT_OK;
  509. goto out_stop;
  510. default:
  511. err_str = "Unknown buffer status code";
  512. }
  513. out_err:
  514. pr_err_ratelimited("%s on CPU %d [PMBSR=0x%016llx, PMBPTR=0x%016llx, PMBLIMITR=0x%016llx]\n",
  515. err_str, smp_processor_id(), pmbsr,
  516. read_sysreg_s(SYS_PMBPTR_EL1),
  517. read_sysreg_s(SYS_PMBLIMITR_EL1));
  518. ret = SPE_PMU_BUF_FAULT_ACT_FATAL;
  519. out_stop:
  520. arm_spe_perf_aux_output_end(handle);
  521. return ret;
  522. }
  523. static irqreturn_t arm_spe_pmu_irq_handler(int irq, void *dev)
  524. {
  525. struct perf_output_handle *handle = dev;
  526. struct perf_event *event = handle->event;
  527. enum arm_spe_pmu_buf_fault_action act;
  528. if (!perf_get_aux(handle))
  529. return IRQ_NONE;
  530. act = arm_spe_pmu_buf_get_fault_act(handle);
  531. if (act == SPE_PMU_BUF_FAULT_ACT_SPURIOUS)
  532. return IRQ_NONE;
  533. /*
  534. * Ensure perf callbacks have completed, which may disable the
  535. * profiling buffer in response to a TRUNCATION flag.
  536. */
  537. irq_work_run();
  538. switch (act) {
  539. case SPE_PMU_BUF_FAULT_ACT_FATAL:
  540. /*
  541. * If a fatal exception occurred then leaving the profiling
  542. * buffer enabled is a recipe waiting to happen. Since
  543. * fatal faults don't always imply truncation, make sure
  544. * that the profiling buffer is disabled explicitly before
  545. * clearing the syndrome register.
  546. */
  547. arm_spe_pmu_disable_and_drain_local();
  548. break;
  549. case SPE_PMU_BUF_FAULT_ACT_OK:
  550. /*
  551. * We handled the fault (the buffer was full), so resume
  552. * profiling as long as we didn't detect truncation.
  553. * PMBPTR might be misaligned, but we'll burn that bridge
  554. * when we get to it.
  555. */
  556. if (!(handle->aux_flags & PERF_AUX_FLAG_TRUNCATED)) {
  557. arm_spe_perf_aux_output_begin(handle, event);
  558. isb();
  559. }
  560. break;
  561. case SPE_PMU_BUF_FAULT_ACT_SPURIOUS:
  562. /* We've seen you before, but GCC has the memory of a sieve. */
  563. break;
  564. }
  565. /* The buffer pointers are now sane, so resume profiling. */
  566. write_sysreg_s(0, SYS_PMBSR_EL1);
  567. return IRQ_HANDLED;
  568. }
  569. static u64 arm_spe_pmsevfr_res0(u16 pmsver)
  570. {
  571. switch (pmsver) {
  572. case ID_AA64DFR0_EL1_PMSVer_IMP:
  573. return PMSEVFR_EL1_RES0_IMP;
  574. case ID_AA64DFR0_EL1_PMSVer_V1P1:
  575. return PMSEVFR_EL1_RES0_V1P1;
  576. case ID_AA64DFR0_EL1_PMSVer_V1P2:
  577. /* Return the highest version we support in default */
  578. default:
  579. return PMSEVFR_EL1_RES0_V1P2;
  580. }
  581. }
  582. /* Perf callbacks */
  583. static int arm_spe_pmu_event_init(struct perf_event *event)
  584. {
  585. u64 reg;
  586. struct perf_event_attr *attr = &event->attr;
  587. struct arm_spe_pmu *spe_pmu = to_spe_pmu(event->pmu);
  588. /* This is, of course, deeply driver-specific */
  589. if (attr->type != event->pmu->type)
  590. return -ENOENT;
  591. if (event->cpu >= 0 &&
  592. !cpumask_test_cpu(event->cpu, &spe_pmu->supported_cpus))
  593. return -ENOENT;
  594. if (arm_spe_event_to_pmsevfr(event) & arm_spe_pmsevfr_res0(spe_pmu->pmsver))
  595. return -EOPNOTSUPP;
  596. if (arm_spe_event_to_pmsnevfr(event) & arm_spe_pmsevfr_res0(spe_pmu->pmsver))
  597. return -EOPNOTSUPP;
  598. if (attr->exclude_idle)
  599. return -EOPNOTSUPP;
  600. /*
  601. * Feedback-directed frequency throttling doesn't work when we
  602. * have a buffer of samples. We'd need to manually count the
  603. * samples in the buffer when it fills up and adjust the event
  604. * count to reflect that. Instead, just force the user to specify
  605. * a sample period.
  606. */
  607. if (attr->freq)
  608. return -EINVAL;
  609. reg = arm_spe_event_to_pmsfcr(event);
  610. if ((FIELD_GET(PMSFCR_EL1_FE, reg)) &&
  611. !(spe_pmu->features & SPE_PMU_FEAT_FILT_EVT))
  612. return -EOPNOTSUPP;
  613. if ((FIELD_GET(PMSFCR_EL1_FnE, reg)) &&
  614. !(spe_pmu->features & SPE_PMU_FEAT_INV_FILT_EVT))
  615. return -EOPNOTSUPP;
  616. if ((FIELD_GET(PMSFCR_EL1_FT, reg)) &&
  617. !(spe_pmu->features & SPE_PMU_FEAT_FILT_TYP))
  618. return -EOPNOTSUPP;
  619. if ((FIELD_GET(PMSFCR_EL1_FL, reg)) &&
  620. !(spe_pmu->features & SPE_PMU_FEAT_FILT_LAT))
  621. return -EOPNOTSUPP;
  622. set_spe_event_has_cx(event);
  623. reg = arm_spe_event_to_pmscr(event);
  624. if (reg & (PMSCR_EL1_PA | PMSCR_EL1_PCT))
  625. return perf_allow_kernel(&event->attr);
  626. return 0;
  627. }
  628. static void arm_spe_pmu_start(struct perf_event *event, int flags)
  629. {
  630. u64 reg;
  631. struct arm_spe_pmu *spe_pmu = to_spe_pmu(event->pmu);
  632. struct hw_perf_event *hwc = &event->hw;
  633. struct perf_output_handle *handle = this_cpu_ptr(spe_pmu->handle);
  634. hwc->state = 0;
  635. arm_spe_perf_aux_output_begin(handle, event);
  636. if (hwc->state)
  637. return;
  638. reg = arm_spe_event_to_pmsfcr(event);
  639. write_sysreg_s(reg, SYS_PMSFCR_EL1);
  640. reg = arm_spe_event_to_pmsevfr(event);
  641. write_sysreg_s(reg, SYS_PMSEVFR_EL1);
  642. if (spe_pmu->features & SPE_PMU_FEAT_INV_FILT_EVT) {
  643. reg = arm_spe_event_to_pmsnevfr(event);
  644. write_sysreg_s(reg, SYS_PMSNEVFR_EL1);
  645. }
  646. reg = arm_spe_event_to_pmslatfr(event);
  647. write_sysreg_s(reg, SYS_PMSLATFR_EL1);
  648. if (flags & PERF_EF_RELOAD) {
  649. reg = arm_spe_event_to_pmsirr(event);
  650. write_sysreg_s(reg, SYS_PMSIRR_EL1);
  651. isb();
  652. reg = local64_read(&hwc->period_left);
  653. write_sysreg_s(reg, SYS_PMSICR_EL1);
  654. }
  655. reg = arm_spe_event_to_pmscr(event);
  656. isb();
  657. write_sysreg_s(reg, SYS_PMSCR_EL1);
  658. }
  659. static void arm_spe_pmu_stop(struct perf_event *event, int flags)
  660. {
  661. struct arm_spe_pmu *spe_pmu = to_spe_pmu(event->pmu);
  662. struct hw_perf_event *hwc = &event->hw;
  663. struct perf_output_handle *handle = this_cpu_ptr(spe_pmu->handle);
  664. /* If we're already stopped, then nothing to do */
  665. if (hwc->state & PERF_HES_STOPPED)
  666. return;
  667. /* Stop all trace generation */
  668. arm_spe_pmu_disable_and_drain_local();
  669. if (flags & PERF_EF_UPDATE) {
  670. /*
  671. * If there's a fault pending then ensure we contain it
  672. * to this buffer, since we might be on the context-switch
  673. * path.
  674. */
  675. if (perf_get_aux(handle)) {
  676. enum arm_spe_pmu_buf_fault_action act;
  677. act = arm_spe_pmu_buf_get_fault_act(handle);
  678. if (act == SPE_PMU_BUF_FAULT_ACT_SPURIOUS)
  679. arm_spe_perf_aux_output_end(handle);
  680. else
  681. write_sysreg_s(0, SYS_PMBSR_EL1);
  682. }
  683. /*
  684. * This may also contain ECOUNT, but nobody else should
  685. * be looking at period_left, since we forbid frequency
  686. * based sampling.
  687. */
  688. local64_set(&hwc->period_left, read_sysreg_s(SYS_PMSICR_EL1));
  689. hwc->state |= PERF_HES_UPTODATE;
  690. }
  691. hwc->state |= PERF_HES_STOPPED;
  692. }
  693. static int arm_spe_pmu_add(struct perf_event *event, int flags)
  694. {
  695. int ret = 0;
  696. struct arm_spe_pmu *spe_pmu = to_spe_pmu(event->pmu);
  697. struct hw_perf_event *hwc = &event->hw;
  698. int cpu = event->cpu == -1 ? smp_processor_id() : event->cpu;
  699. if (!cpumask_test_cpu(cpu, &spe_pmu->supported_cpus))
  700. return -ENOENT;
  701. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  702. if (flags & PERF_EF_START) {
  703. arm_spe_pmu_start(event, PERF_EF_RELOAD);
  704. if (hwc->state & PERF_HES_STOPPED)
  705. ret = -EINVAL;
  706. }
  707. return ret;
  708. }
  709. static void arm_spe_pmu_del(struct perf_event *event, int flags)
  710. {
  711. arm_spe_pmu_stop(event, PERF_EF_UPDATE);
  712. }
  713. static void arm_spe_pmu_read(struct perf_event *event)
  714. {
  715. }
  716. static void *arm_spe_pmu_setup_aux(struct perf_event *event, void **pages,
  717. int nr_pages, bool snapshot)
  718. {
  719. int i, cpu = event->cpu;
  720. struct page **pglist;
  721. struct arm_spe_pmu_buf *buf;
  722. /* We need at least two pages for this to work. */
  723. if (nr_pages < 2)
  724. return NULL;
  725. /*
  726. * We require an even number of pages for snapshot mode, so that
  727. * we can effectively treat the buffer as consisting of two equal
  728. * parts and give userspace a fighting chance of getting some
  729. * useful data out of it.
  730. */
  731. if (snapshot && (nr_pages & 1))
  732. return NULL;
  733. if (cpu == -1)
  734. cpu = raw_smp_processor_id();
  735. buf = kzalloc_node(sizeof(*buf), GFP_KERNEL, cpu_to_node(cpu));
  736. if (!buf)
  737. return NULL;
  738. pglist = kcalloc(nr_pages, sizeof(*pglist), GFP_KERNEL);
  739. if (!pglist)
  740. goto out_free_buf;
  741. for (i = 0; i < nr_pages; ++i)
  742. pglist[i] = virt_to_page(pages[i]);
  743. buf->base = vmap(pglist, nr_pages, VM_MAP, PAGE_KERNEL);
  744. if (!buf->base)
  745. goto out_free_pglist;
  746. buf->nr_pages = nr_pages;
  747. buf->snapshot = snapshot;
  748. kfree(pglist);
  749. return buf;
  750. out_free_pglist:
  751. kfree(pglist);
  752. out_free_buf:
  753. kfree(buf);
  754. return NULL;
  755. }
  756. static void arm_spe_pmu_free_aux(void *aux)
  757. {
  758. struct arm_spe_pmu_buf *buf = aux;
  759. vunmap(buf->base);
  760. kfree(buf);
  761. }
  762. /* Initialisation and teardown functions */
  763. static int arm_spe_pmu_perf_init(struct arm_spe_pmu *spe_pmu)
  764. {
  765. static atomic_t pmu_idx = ATOMIC_INIT(-1);
  766. int idx;
  767. char *name;
  768. struct device *dev = &spe_pmu->pdev->dev;
  769. spe_pmu->pmu = (struct pmu) {
  770. .module = THIS_MODULE,
  771. .parent = &spe_pmu->pdev->dev,
  772. .capabilities = PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE,
  773. .attr_groups = arm_spe_pmu_attr_groups,
  774. /*
  775. * We hitch a ride on the software context here, so that
  776. * we can support per-task profiling (which is not possible
  777. * with the invalid context as it doesn't get sched callbacks).
  778. * This requires that userspace either uses a dummy event for
  779. * perf_event_open, since the aux buffer is not setup until
  780. * a subsequent mmap, or creates the profiling event in a
  781. * disabled state and explicitly PERF_EVENT_IOC_ENABLEs it
  782. * once the buffer has been created.
  783. */
  784. .task_ctx_nr = perf_sw_context,
  785. .event_init = arm_spe_pmu_event_init,
  786. .add = arm_spe_pmu_add,
  787. .del = arm_spe_pmu_del,
  788. .start = arm_spe_pmu_start,
  789. .stop = arm_spe_pmu_stop,
  790. .read = arm_spe_pmu_read,
  791. .setup_aux = arm_spe_pmu_setup_aux,
  792. .free_aux = arm_spe_pmu_free_aux,
  793. };
  794. idx = atomic_inc_return(&pmu_idx);
  795. name = devm_kasprintf(dev, GFP_KERNEL, "%s_%d", PMUNAME, idx);
  796. if (!name) {
  797. dev_err(dev, "failed to allocate name for pmu %d\n", idx);
  798. return -ENOMEM;
  799. }
  800. return perf_pmu_register(&spe_pmu->pmu, name, -1);
  801. }
  802. static void arm_spe_pmu_perf_destroy(struct arm_spe_pmu *spe_pmu)
  803. {
  804. perf_pmu_unregister(&spe_pmu->pmu);
  805. }
  806. static void __arm_spe_pmu_dev_probe(void *info)
  807. {
  808. int fld;
  809. u64 reg;
  810. struct arm_spe_pmu *spe_pmu = info;
  811. struct device *dev = &spe_pmu->pdev->dev;
  812. fld = cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64DFR0_EL1),
  813. ID_AA64DFR0_EL1_PMSVer_SHIFT);
  814. if (!fld) {
  815. dev_err(dev,
  816. "unsupported ID_AA64DFR0_EL1.PMSVer [%d] on CPU %d\n",
  817. fld, smp_processor_id());
  818. return;
  819. }
  820. spe_pmu->pmsver = (u16)fld;
  821. /* Read PMBIDR first to determine whether or not we have access */
  822. reg = read_sysreg_s(SYS_PMBIDR_EL1);
  823. if (FIELD_GET(PMBIDR_EL1_P, reg)) {
  824. dev_err(dev,
  825. "profiling buffer owned by higher exception level\n");
  826. return;
  827. }
  828. /* Minimum alignment. If it's out-of-range, then fail the probe */
  829. fld = FIELD_GET(PMBIDR_EL1_ALIGN, reg);
  830. spe_pmu->align = 1 << fld;
  831. if (spe_pmu->align > SZ_2K) {
  832. dev_err(dev, "unsupported PMBIDR.Align [%d] on CPU %d\n",
  833. fld, smp_processor_id());
  834. return;
  835. }
  836. /* It's now safe to read PMSIDR and figure out what we've got */
  837. reg = read_sysreg_s(SYS_PMSIDR_EL1);
  838. if (FIELD_GET(PMSIDR_EL1_FE, reg))
  839. spe_pmu->features |= SPE_PMU_FEAT_FILT_EVT;
  840. if (FIELD_GET(PMSIDR_EL1_FnE, reg))
  841. spe_pmu->features |= SPE_PMU_FEAT_INV_FILT_EVT;
  842. if (FIELD_GET(PMSIDR_EL1_FT, reg))
  843. spe_pmu->features |= SPE_PMU_FEAT_FILT_TYP;
  844. if (FIELD_GET(PMSIDR_EL1_FL, reg))
  845. spe_pmu->features |= SPE_PMU_FEAT_FILT_LAT;
  846. if (FIELD_GET(PMSIDR_EL1_ARCHINST, reg))
  847. spe_pmu->features |= SPE_PMU_FEAT_ARCH_INST;
  848. if (FIELD_GET(PMSIDR_EL1_LDS, reg))
  849. spe_pmu->features |= SPE_PMU_FEAT_LDS;
  850. if (FIELD_GET(PMSIDR_EL1_ERND, reg))
  851. spe_pmu->features |= SPE_PMU_FEAT_ERND;
  852. /* This field has a spaced out encoding, so just use a look-up */
  853. fld = FIELD_GET(PMSIDR_EL1_INTERVAL, reg);
  854. switch (fld) {
  855. case PMSIDR_EL1_INTERVAL_256:
  856. spe_pmu->min_period = 256;
  857. break;
  858. case PMSIDR_EL1_INTERVAL_512:
  859. spe_pmu->min_period = 512;
  860. break;
  861. case PMSIDR_EL1_INTERVAL_768:
  862. spe_pmu->min_period = 768;
  863. break;
  864. case PMSIDR_EL1_INTERVAL_1024:
  865. spe_pmu->min_period = 1024;
  866. break;
  867. case PMSIDR_EL1_INTERVAL_1536:
  868. spe_pmu->min_period = 1536;
  869. break;
  870. case PMSIDR_EL1_INTERVAL_2048:
  871. spe_pmu->min_period = 2048;
  872. break;
  873. case PMSIDR_EL1_INTERVAL_3072:
  874. spe_pmu->min_period = 3072;
  875. break;
  876. default:
  877. dev_warn(dev, "unknown PMSIDR_EL1.Interval [%d]; assuming 8\n",
  878. fld);
  879. fallthrough;
  880. case PMSIDR_EL1_INTERVAL_4096:
  881. spe_pmu->min_period = 4096;
  882. }
  883. /* Maximum record size. If it's out-of-range, then fail the probe */
  884. fld = FIELD_GET(PMSIDR_EL1_MAXSIZE, reg);
  885. spe_pmu->max_record_sz = 1 << fld;
  886. if (spe_pmu->max_record_sz > SZ_2K || spe_pmu->max_record_sz < 16) {
  887. dev_err(dev, "unsupported PMSIDR_EL1.MaxSize [%d] on CPU %d\n",
  888. fld, smp_processor_id());
  889. return;
  890. }
  891. fld = FIELD_GET(PMSIDR_EL1_COUNTSIZE, reg);
  892. switch (fld) {
  893. default:
  894. dev_warn(dev, "unknown PMSIDR_EL1.CountSize [%d]; assuming 2\n",
  895. fld);
  896. fallthrough;
  897. case PMSIDR_EL1_COUNTSIZE_12_BIT_SAT:
  898. spe_pmu->counter_sz = 12;
  899. break;
  900. case PMSIDR_EL1_COUNTSIZE_16_BIT_SAT:
  901. spe_pmu->counter_sz = 16;
  902. }
  903. dev_info(dev,
  904. "probed SPEv1.%d for CPUs %*pbl [max_record_sz %u, align %u, features 0x%llx]\n",
  905. spe_pmu->pmsver - 1, cpumask_pr_args(&spe_pmu->supported_cpus),
  906. spe_pmu->max_record_sz, spe_pmu->align, spe_pmu->features);
  907. spe_pmu->features |= SPE_PMU_FEAT_DEV_PROBED;
  908. }
  909. static void __arm_spe_pmu_reset_local(void)
  910. {
  911. /*
  912. * This is probably overkill, as we have no idea where we're
  913. * draining any buffered data to...
  914. */
  915. arm_spe_pmu_disable_and_drain_local();
  916. /* Reset the buffer base pointer */
  917. write_sysreg_s(0, SYS_PMBPTR_EL1);
  918. isb();
  919. /* Clear any pending management interrupts */
  920. write_sysreg_s(0, SYS_PMBSR_EL1);
  921. isb();
  922. }
  923. static void __arm_spe_pmu_setup_one(void *info)
  924. {
  925. struct arm_spe_pmu *spe_pmu = info;
  926. __arm_spe_pmu_reset_local();
  927. enable_percpu_irq(spe_pmu->irq, IRQ_TYPE_NONE);
  928. }
  929. static void __arm_spe_pmu_stop_one(void *info)
  930. {
  931. struct arm_spe_pmu *spe_pmu = info;
  932. disable_percpu_irq(spe_pmu->irq);
  933. __arm_spe_pmu_reset_local();
  934. }
  935. static int arm_spe_pmu_cpu_startup(unsigned int cpu, struct hlist_node *node)
  936. {
  937. struct arm_spe_pmu *spe_pmu;
  938. spe_pmu = hlist_entry_safe(node, struct arm_spe_pmu, hotplug_node);
  939. if (!cpumask_test_cpu(cpu, &spe_pmu->supported_cpus))
  940. return 0;
  941. __arm_spe_pmu_setup_one(spe_pmu);
  942. return 0;
  943. }
  944. static int arm_spe_pmu_cpu_teardown(unsigned int cpu, struct hlist_node *node)
  945. {
  946. struct arm_spe_pmu *spe_pmu;
  947. spe_pmu = hlist_entry_safe(node, struct arm_spe_pmu, hotplug_node);
  948. if (!cpumask_test_cpu(cpu, &spe_pmu->supported_cpus))
  949. return 0;
  950. __arm_spe_pmu_stop_one(spe_pmu);
  951. return 0;
  952. }
  953. static int arm_spe_pmu_dev_init(struct arm_spe_pmu *spe_pmu)
  954. {
  955. int ret;
  956. cpumask_t *mask = &spe_pmu->supported_cpus;
  957. /* Make sure we probe the hardware on a relevant CPU */
  958. ret = smp_call_function_any(mask, __arm_spe_pmu_dev_probe, spe_pmu, 1);
  959. if (ret || !(spe_pmu->features & SPE_PMU_FEAT_DEV_PROBED))
  960. return -ENXIO;
  961. /* Request our PPIs (note that the IRQ is still disabled) */
  962. ret = request_percpu_irq(spe_pmu->irq, arm_spe_pmu_irq_handler, DRVNAME,
  963. spe_pmu->handle);
  964. if (ret)
  965. return ret;
  966. /*
  967. * Register our hotplug notifier now so we don't miss any events.
  968. * This will enable the IRQ for any supported CPUs that are already
  969. * up.
  970. */
  971. ret = cpuhp_state_add_instance(arm_spe_pmu_online,
  972. &spe_pmu->hotplug_node);
  973. if (ret)
  974. free_percpu_irq(spe_pmu->irq, spe_pmu->handle);
  975. return ret;
  976. }
  977. static void arm_spe_pmu_dev_teardown(struct arm_spe_pmu *spe_pmu)
  978. {
  979. cpuhp_state_remove_instance(arm_spe_pmu_online, &spe_pmu->hotplug_node);
  980. free_percpu_irq(spe_pmu->irq, spe_pmu->handle);
  981. }
  982. /* Driver and device probing */
  983. static int arm_spe_pmu_irq_probe(struct arm_spe_pmu *spe_pmu)
  984. {
  985. struct platform_device *pdev = spe_pmu->pdev;
  986. int irq = platform_get_irq(pdev, 0);
  987. if (irq < 0)
  988. return -ENXIO;
  989. if (!irq_is_percpu(irq)) {
  990. dev_err(&pdev->dev, "expected PPI but got SPI (%d)\n", irq);
  991. return -EINVAL;
  992. }
  993. if (irq_get_percpu_devid_partition(irq, &spe_pmu->supported_cpus)) {
  994. dev_err(&pdev->dev, "failed to get PPI partition (%d)\n", irq);
  995. return -EINVAL;
  996. }
  997. spe_pmu->irq = irq;
  998. return 0;
  999. }
  1000. static const struct of_device_id arm_spe_pmu_of_match[] = {
  1001. { .compatible = "arm,statistical-profiling-extension-v1", .data = (void *)1 },
  1002. { /* Sentinel */ },
  1003. };
  1004. MODULE_DEVICE_TABLE(of, arm_spe_pmu_of_match);
  1005. static const struct platform_device_id arm_spe_match[] = {
  1006. { ARMV8_SPE_PDEV_NAME, 0},
  1007. { }
  1008. };
  1009. MODULE_DEVICE_TABLE(platform, arm_spe_match);
  1010. static int arm_spe_pmu_device_probe(struct platform_device *pdev)
  1011. {
  1012. int ret;
  1013. struct arm_spe_pmu *spe_pmu;
  1014. struct device *dev = &pdev->dev;
  1015. /*
  1016. * If kernelspace is unmapped when running at EL0, then the SPE
  1017. * buffer will fault and prematurely terminate the AUX session.
  1018. */
  1019. if (arm64_kernel_unmapped_at_el0()) {
  1020. dev_warn_once(dev, "profiling buffer inaccessible. Try passing \"kpti=off\" on the kernel command line\n");
  1021. return -EPERM;
  1022. }
  1023. spe_pmu = devm_kzalloc(dev, sizeof(*spe_pmu), GFP_KERNEL);
  1024. if (!spe_pmu)
  1025. return -ENOMEM;
  1026. spe_pmu->handle = alloc_percpu(typeof(*spe_pmu->handle));
  1027. if (!spe_pmu->handle)
  1028. return -ENOMEM;
  1029. spe_pmu->pdev = pdev;
  1030. platform_set_drvdata(pdev, spe_pmu);
  1031. ret = arm_spe_pmu_irq_probe(spe_pmu);
  1032. if (ret)
  1033. goto out_free_handle;
  1034. ret = arm_spe_pmu_dev_init(spe_pmu);
  1035. if (ret)
  1036. goto out_free_handle;
  1037. ret = arm_spe_pmu_perf_init(spe_pmu);
  1038. if (ret)
  1039. goto out_teardown_dev;
  1040. return 0;
  1041. out_teardown_dev:
  1042. arm_spe_pmu_dev_teardown(spe_pmu);
  1043. out_free_handle:
  1044. free_percpu(spe_pmu->handle);
  1045. return ret;
  1046. }
  1047. static void arm_spe_pmu_device_remove(struct platform_device *pdev)
  1048. {
  1049. struct arm_spe_pmu *spe_pmu = platform_get_drvdata(pdev);
  1050. arm_spe_pmu_perf_destroy(spe_pmu);
  1051. arm_spe_pmu_dev_teardown(spe_pmu);
  1052. free_percpu(spe_pmu->handle);
  1053. }
  1054. static struct platform_driver arm_spe_pmu_driver = {
  1055. .id_table = arm_spe_match,
  1056. .driver = {
  1057. .name = DRVNAME,
  1058. .of_match_table = of_match_ptr(arm_spe_pmu_of_match),
  1059. .suppress_bind_attrs = true,
  1060. },
  1061. .probe = arm_spe_pmu_device_probe,
  1062. .remove_new = arm_spe_pmu_device_remove,
  1063. };
  1064. static int __init arm_spe_pmu_init(void)
  1065. {
  1066. int ret;
  1067. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, DRVNAME,
  1068. arm_spe_pmu_cpu_startup,
  1069. arm_spe_pmu_cpu_teardown);
  1070. if (ret < 0)
  1071. return ret;
  1072. arm_spe_pmu_online = ret;
  1073. ret = platform_driver_register(&arm_spe_pmu_driver);
  1074. if (ret)
  1075. cpuhp_remove_multi_state(arm_spe_pmu_online);
  1076. return ret;
  1077. }
  1078. static void __exit arm_spe_pmu_exit(void)
  1079. {
  1080. platform_driver_unregister(&arm_spe_pmu_driver);
  1081. cpuhp_remove_multi_state(arm_spe_pmu_online);
  1082. }
  1083. module_init(arm_spe_pmu_init);
  1084. module_exit(arm_spe_pmu_exit);
  1085. MODULE_DESCRIPTION("Perf driver for the ARMv8.2 Statistical Profiling Extension");
  1086. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  1087. MODULE_LICENSE("GPL v2");