arm_spe_pmu.c 34 KB

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