arm_spe_pmu.c 40 KB

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