arm_spe_pmu.c 34 KB

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