arm_pmuv3.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ARMv8 PMUv3 Performance Events handling code.
  4. *
  5. * Copyright (C) 2012 ARM Limited
  6. * Author: Will Deacon <will.deacon@arm.com>
  7. *
  8. * This code is based heavily on the ARMv7 perf event code.
  9. */
  10. #include <asm/irq_regs.h>
  11. #include <asm/perf_event.h>
  12. #include <asm/virt.h>
  13. #include <clocksource/arm_arch_timer.h>
  14. #include <linux/acpi.h>
  15. #include <linux/bitfield.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/of.h>
  18. #include <linux/perf/arm_pmu.h>
  19. #include <linux/perf/arm_pmuv3.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sched_clock.h>
  22. #include <linux/smp.h>
  23. #include <linux/nmi.h>
  24. #include "arm_brbe.h"
  25. /* ARMv8 Cortex-A53 specific event types. */
  26. #define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2
  27. /* ARMv8 Cavium ThunderX specific event types. */
  28. #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST 0xE9
  29. #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS 0xEA
  30. #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS 0xEB
  31. #define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC
  32. #define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED
  33. /*
  34. * ARMv8 Architectural defined events, not all of these may
  35. * be supported on any given implementation. Unsupported events will
  36. * be disabled at run-time based on the PMCEID registers.
  37. */
  38. static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
  39. PERF_MAP_ALL_UNSUPPORTED,
  40. [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
  41. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
  42. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
  43. [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
  44. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
  45. [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
  46. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
  47. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
  48. };
  49. static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  50. [PERF_COUNT_HW_CACHE_OP_MAX]
  51. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  52. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  53. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
  54. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
  55. [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
  56. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
  57. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL,
  58. [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB,
  59. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
  60. [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB,
  61. [C(LL)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD,
  62. [C(LL)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_LL_CACHE_RD,
  63. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
  64. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
  65. };
  66. static const unsigned armv8_a53_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  67. [PERF_COUNT_HW_CACHE_OP_MAX]
  68. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  69. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  70. [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
  71. [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
  72. [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
  73. };
  74. static const unsigned armv8_a57_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  75. [PERF_COUNT_HW_CACHE_OP_MAX]
  76. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  77. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  78. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
  79. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
  80. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
  81. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
  82. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
  83. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
  84. [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
  85. [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
  86. };
  87. static const unsigned armv8_a73_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  88. [PERF_COUNT_HW_CACHE_OP_MAX]
  89. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  90. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  91. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
  92. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
  93. };
  94. static const unsigned armv8_thunder_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  95. [PERF_COUNT_HW_CACHE_OP_MAX]
  96. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  97. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  98. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
  99. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
  100. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
  101. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST,
  102. [C(L1D)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS,
  103. [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS,
  104. [C(L1I)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS,
  105. [C(L1I)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS,
  106. [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
  107. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
  108. [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
  109. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
  110. };
  111. static const unsigned armv8_vulcan_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  112. [PERF_COUNT_HW_CACHE_OP_MAX]
  113. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  114. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  115. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
  116. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
  117. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
  118. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
  119. [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
  120. [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
  121. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
  122. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
  123. [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
  124. [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
  125. };
  126. static ssize_t
  127. armv8pmu_events_sysfs_show(struct device *dev,
  128. struct device_attribute *attr, char *page)
  129. {
  130. struct perf_pmu_events_attr *pmu_attr;
  131. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  132. return sprintf(page, "event=0x%04llx\n", pmu_attr->id);
  133. }
  134. #define ARMV8_EVENT_ATTR(name, config) \
  135. PMU_EVENT_ATTR_ID(name, armv8pmu_events_sysfs_show, config)
  136. static struct attribute *armv8_pmuv3_event_attrs[] = {
  137. /*
  138. * Don't expose the sw_incr event in /sys. It's not usable as writes to
  139. * PMSWINC_EL0 will trap as PMUSERENR.{SW,EN}=={0,0} and event rotation
  140. * means we don't have a fixed event<->counter relationship regardless.
  141. */
  142. ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL),
  143. ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL),
  144. ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL),
  145. ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE),
  146. ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL),
  147. ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED),
  148. ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED),
  149. ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED),
  150. ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN),
  151. ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN),
  152. ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED),
  153. ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED),
  154. ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED),
  155. ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED),
  156. ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED),
  157. ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED),
  158. ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES),
  159. ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED),
  160. ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS),
  161. ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE),
  162. ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB),
  163. ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE),
  164. ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL),
  165. ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB),
  166. ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS),
  167. ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR),
  168. ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC),
  169. ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED),
  170. ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES),
  171. /* Don't expose the chain event in /sys, since it's useless in isolation */
  172. ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE),
  173. ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE),
  174. ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED),
  175. ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED),
  176. ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND),
  177. ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND),
  178. ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB),
  179. ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB),
  180. ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE),
  181. ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL),
  182. ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE),
  183. ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL),
  184. ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE),
  185. ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB),
  186. ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL),
  187. ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL),
  188. ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB),
  189. ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB),
  190. ARMV8_EVENT_ATTR(remote_access, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS),
  191. ARMV8_EVENT_ATTR(ll_cache, ARMV8_PMUV3_PERFCTR_LL_CACHE),
  192. ARMV8_EVENT_ATTR(ll_cache_miss, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS),
  193. ARMV8_EVENT_ATTR(dtlb_walk, ARMV8_PMUV3_PERFCTR_DTLB_WALK),
  194. ARMV8_EVENT_ATTR(itlb_walk, ARMV8_PMUV3_PERFCTR_ITLB_WALK),
  195. ARMV8_EVENT_ATTR(ll_cache_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_RD),
  196. ARMV8_EVENT_ATTR(ll_cache_miss_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD),
  197. ARMV8_EVENT_ATTR(remote_access_rd, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS_RD),
  198. ARMV8_EVENT_ATTR(l1d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L1D_CACHE_LMISS_RD),
  199. ARMV8_EVENT_ATTR(op_retired, ARMV8_PMUV3_PERFCTR_OP_RETIRED),
  200. ARMV8_EVENT_ATTR(op_spec, ARMV8_PMUV3_PERFCTR_OP_SPEC),
  201. ARMV8_EVENT_ATTR(stall, ARMV8_PMUV3_PERFCTR_STALL),
  202. ARMV8_EVENT_ATTR(stall_slot_backend, ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND),
  203. ARMV8_EVENT_ATTR(stall_slot_frontend, ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND),
  204. ARMV8_EVENT_ATTR(stall_slot, ARMV8_PMUV3_PERFCTR_STALL_SLOT),
  205. ARMV8_EVENT_ATTR(sample_pop, ARMV8_SPE_PERFCTR_SAMPLE_POP),
  206. ARMV8_EVENT_ATTR(sample_feed, ARMV8_SPE_PERFCTR_SAMPLE_FEED),
  207. ARMV8_EVENT_ATTR(sample_filtrate, ARMV8_SPE_PERFCTR_SAMPLE_FILTRATE),
  208. ARMV8_EVENT_ATTR(sample_collision, ARMV8_SPE_PERFCTR_SAMPLE_COLLISION),
  209. ARMV8_EVENT_ATTR(cnt_cycles, ARMV8_AMU_PERFCTR_CNT_CYCLES),
  210. ARMV8_EVENT_ATTR(stall_backend_mem, ARMV8_AMU_PERFCTR_STALL_BACKEND_MEM),
  211. ARMV8_EVENT_ATTR(l1i_cache_lmiss, ARMV8_PMUV3_PERFCTR_L1I_CACHE_LMISS),
  212. ARMV8_EVENT_ATTR(l2d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L2D_CACHE_LMISS_RD),
  213. ARMV8_EVENT_ATTR(l2i_cache_lmiss, ARMV8_PMUV3_PERFCTR_L2I_CACHE_LMISS),
  214. ARMV8_EVENT_ATTR(l3d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L3D_CACHE_LMISS_RD),
  215. ARMV8_EVENT_ATTR(trb_wrap, ARMV8_PMUV3_PERFCTR_TRB_WRAP),
  216. ARMV8_EVENT_ATTR(trb_trig, ARMV8_PMUV3_PERFCTR_TRB_TRIG),
  217. ARMV8_EVENT_ATTR(trcextout0, ARMV8_PMUV3_PERFCTR_TRCEXTOUT0),
  218. ARMV8_EVENT_ATTR(trcextout1, ARMV8_PMUV3_PERFCTR_TRCEXTOUT1),
  219. ARMV8_EVENT_ATTR(trcextout2, ARMV8_PMUV3_PERFCTR_TRCEXTOUT2),
  220. ARMV8_EVENT_ATTR(trcextout3, ARMV8_PMUV3_PERFCTR_TRCEXTOUT3),
  221. ARMV8_EVENT_ATTR(cti_trigout4, ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT4),
  222. ARMV8_EVENT_ATTR(cti_trigout5, ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT5),
  223. ARMV8_EVENT_ATTR(cti_trigout6, ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT6),
  224. ARMV8_EVENT_ATTR(cti_trigout7, ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT7),
  225. ARMV8_EVENT_ATTR(ldst_align_lat, ARMV8_PMUV3_PERFCTR_LDST_ALIGN_LAT),
  226. ARMV8_EVENT_ATTR(ld_align_lat, ARMV8_PMUV3_PERFCTR_LD_ALIGN_LAT),
  227. ARMV8_EVENT_ATTR(st_align_lat, ARMV8_PMUV3_PERFCTR_ST_ALIGN_LAT),
  228. ARMV8_EVENT_ATTR(mem_access_checked, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED),
  229. ARMV8_EVENT_ATTR(mem_access_checked_rd, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_RD),
  230. ARMV8_EVENT_ATTR(mem_access_checked_wr, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_WR),
  231. NULL,
  232. };
  233. static umode_t
  234. armv8pmu_event_attr_is_visible(struct kobject *kobj,
  235. struct attribute *attr, int unused)
  236. {
  237. struct device *dev = kobj_to_dev(kobj);
  238. struct pmu *pmu = dev_get_drvdata(dev);
  239. struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
  240. struct perf_pmu_events_attr *pmu_attr;
  241. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
  242. if (pmu_attr->id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
  243. test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
  244. return attr->mode;
  245. if (pmu_attr->id >= ARMV8_PMUV3_EXT_COMMON_EVENT_BASE) {
  246. u64 id = pmu_attr->id - ARMV8_PMUV3_EXT_COMMON_EVENT_BASE;
  247. if (id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
  248. test_bit(id, cpu_pmu->pmceid_ext_bitmap))
  249. return attr->mode;
  250. }
  251. return 0;
  252. }
  253. static const struct attribute_group armv8_pmuv3_events_attr_group = {
  254. .name = "events",
  255. .attrs = armv8_pmuv3_event_attrs,
  256. .is_visible = armv8pmu_event_attr_is_visible,
  257. };
  258. /* User ABI */
  259. #define ATTR_CFG_FLD_event_CFG config
  260. #define ATTR_CFG_FLD_event_LO 0
  261. #define ATTR_CFG_FLD_event_HI 15
  262. #define ATTR_CFG_FLD_long_CFG config1
  263. #define ATTR_CFG_FLD_long_LO 0
  264. #define ATTR_CFG_FLD_long_HI 0
  265. #define ATTR_CFG_FLD_rdpmc_CFG config1
  266. #define ATTR_CFG_FLD_rdpmc_LO 1
  267. #define ATTR_CFG_FLD_rdpmc_HI 1
  268. #define ATTR_CFG_FLD_threshold_count_CFG config1 /* PMEVTYPER.TC[0] */
  269. #define ATTR_CFG_FLD_threshold_count_LO 2
  270. #define ATTR_CFG_FLD_threshold_count_HI 2
  271. #define ATTR_CFG_FLD_threshold_compare_CFG config1 /* PMEVTYPER.TC[2:1] */
  272. #define ATTR_CFG_FLD_threshold_compare_LO 3
  273. #define ATTR_CFG_FLD_threshold_compare_HI 4
  274. #define ATTR_CFG_FLD_threshold_CFG config1 /* PMEVTYPER.TH */
  275. #define ATTR_CFG_FLD_threshold_LO 5
  276. #define ATTR_CFG_FLD_threshold_HI 16
  277. GEN_PMU_FORMAT_ATTR(event);
  278. GEN_PMU_FORMAT_ATTR(long);
  279. GEN_PMU_FORMAT_ATTR(rdpmc);
  280. GEN_PMU_FORMAT_ATTR(threshold_count);
  281. GEN_PMU_FORMAT_ATTR(threshold_compare);
  282. GEN_PMU_FORMAT_ATTR(threshold);
  283. static int sysctl_perf_user_access __read_mostly;
  284. static bool armv8pmu_event_is_64bit(struct perf_event *event)
  285. {
  286. return ATTR_CFG_GET_FLD(&event->attr, long);
  287. }
  288. static bool armv8pmu_event_want_user_access(struct perf_event *event)
  289. {
  290. return ATTR_CFG_GET_FLD(&event->attr, rdpmc);
  291. }
  292. static u32 armv8pmu_event_get_threshold(struct perf_event_attr *attr)
  293. {
  294. return ATTR_CFG_GET_FLD(attr, threshold);
  295. }
  296. static u8 armv8pmu_event_threshold_control(struct perf_event_attr *attr)
  297. {
  298. u8 th_compare = ATTR_CFG_GET_FLD(attr, threshold_compare);
  299. u8 th_count = ATTR_CFG_GET_FLD(attr, threshold_count);
  300. /*
  301. * The count bit is always the bottom bit of the full control field, and
  302. * the comparison is the upper two bits, but it's not explicitly
  303. * labelled in the Arm ARM. For the Perf interface we split it into two
  304. * fields, so reconstruct it here.
  305. */
  306. return (th_compare << 1) | th_count;
  307. }
  308. static struct attribute *armv8_pmuv3_format_attrs[] = {
  309. &format_attr_event.attr,
  310. &format_attr_long.attr,
  311. &format_attr_rdpmc.attr,
  312. &format_attr_threshold.attr,
  313. &format_attr_threshold_compare.attr,
  314. &format_attr_threshold_count.attr,
  315. NULL,
  316. };
  317. static const struct attribute_group armv8_pmuv3_format_attr_group = {
  318. .name = "format",
  319. .attrs = armv8_pmuv3_format_attrs,
  320. };
  321. static ssize_t slots_show(struct device *dev, struct device_attribute *attr,
  322. char *page)
  323. {
  324. struct pmu *pmu = dev_get_drvdata(dev);
  325. struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
  326. u32 slots = FIELD_GET(ARMV8_PMU_SLOTS, cpu_pmu->reg_pmmir);
  327. return sysfs_emit(page, "0x%08x\n", slots);
  328. }
  329. static DEVICE_ATTR_RO(slots);
  330. static ssize_t bus_slots_show(struct device *dev, struct device_attribute *attr,
  331. char *page)
  332. {
  333. struct pmu *pmu = dev_get_drvdata(dev);
  334. struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
  335. u32 bus_slots = FIELD_GET(ARMV8_PMU_BUS_SLOTS, cpu_pmu->reg_pmmir);
  336. return sysfs_emit(page, "0x%08x\n", bus_slots);
  337. }
  338. static DEVICE_ATTR_RO(bus_slots);
  339. static ssize_t bus_width_show(struct device *dev, struct device_attribute *attr,
  340. char *page)
  341. {
  342. struct pmu *pmu = dev_get_drvdata(dev);
  343. struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
  344. u32 bus_width = FIELD_GET(ARMV8_PMU_BUS_WIDTH, cpu_pmu->reg_pmmir);
  345. u32 val = 0;
  346. /* Encoded as Log2(number of bytes), plus one */
  347. if (bus_width > 2 && bus_width < 13)
  348. val = 1 << (bus_width - 1);
  349. return sysfs_emit(page, "0x%08x\n", val);
  350. }
  351. static DEVICE_ATTR_RO(bus_width);
  352. static u32 threshold_max(struct arm_pmu *cpu_pmu)
  353. {
  354. /*
  355. * PMMIR.THWIDTH is readable and non-zero on aarch32, but it would be
  356. * impossible to write the threshold in the upper 32 bits of PMEVTYPER.
  357. */
  358. if (IS_ENABLED(CONFIG_ARM))
  359. return 0;
  360. /*
  361. * The largest value that can be written to PMEVTYPER<n>_EL0.TH is
  362. * (2 ^ PMMIR.THWIDTH) - 1.
  363. */
  364. return (1 << FIELD_GET(ARMV8_PMU_THWIDTH, cpu_pmu->reg_pmmir)) - 1;
  365. }
  366. static ssize_t threshold_max_show(struct device *dev,
  367. struct device_attribute *attr, char *page)
  368. {
  369. struct pmu *pmu = dev_get_drvdata(dev);
  370. struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
  371. return sysfs_emit(page, "0x%08x\n", threshold_max(cpu_pmu));
  372. }
  373. static DEVICE_ATTR_RO(threshold_max);
  374. static ssize_t branches_show(struct device *dev,
  375. struct device_attribute *attr, char *page)
  376. {
  377. struct pmu *pmu = dev_get_drvdata(dev);
  378. struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
  379. return sysfs_emit(page, "%d\n", brbe_num_branch_records(cpu_pmu));
  380. }
  381. static DEVICE_ATTR_RO(branches);
  382. static struct attribute *armv8_pmuv3_caps_attrs[] = {
  383. &dev_attr_branches.attr,
  384. &dev_attr_slots.attr,
  385. &dev_attr_bus_slots.attr,
  386. &dev_attr_bus_width.attr,
  387. &dev_attr_threshold_max.attr,
  388. NULL,
  389. };
  390. static umode_t caps_is_visible(struct kobject *kobj, struct attribute *attr, int i)
  391. {
  392. struct device *dev = kobj_to_dev(kobj);
  393. struct pmu *pmu = dev_get_drvdata(dev);
  394. struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
  395. if (i == 0)
  396. return brbe_num_branch_records(cpu_pmu) ? attr->mode : 0;
  397. return attr->mode;
  398. }
  399. static const struct attribute_group armv8_pmuv3_caps_attr_group = {
  400. .name = "caps",
  401. .attrs = armv8_pmuv3_caps_attrs,
  402. .is_visible = caps_is_visible,
  403. };
  404. /*
  405. * We unconditionally enable ARMv8.5-PMU long event counter support
  406. * (64-bit events) where supported. Indicate if this arm_pmu has long
  407. * event counter support.
  408. *
  409. * On AArch32, long counters make no sense (you can't access the top
  410. * bits), so we only enable this on AArch64.
  411. */
  412. static bool armv8pmu_has_long_event(struct arm_pmu *cpu_pmu)
  413. {
  414. return (IS_ENABLED(CONFIG_ARM64) && is_pmuv3p5(cpu_pmu->pmuver));
  415. }
  416. static bool armv8pmu_event_has_user_read(struct perf_event *event)
  417. {
  418. return event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT;
  419. }
  420. /*
  421. * We must chain two programmable counters for 64 bit events,
  422. * except when we have allocated the 64bit cycle counter (for CPU
  423. * cycles event) or when user space counter access is enabled.
  424. */
  425. static bool armv8pmu_event_is_chained(struct perf_event *event)
  426. {
  427. int idx = event->hw.idx;
  428. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  429. return !armv8pmu_event_has_user_read(event) &&
  430. armv8pmu_event_is_64bit(event) &&
  431. !armv8pmu_has_long_event(cpu_pmu) &&
  432. (idx < ARMV8_PMU_MAX_GENERAL_COUNTERS);
  433. }
  434. /*
  435. * ARMv8 low level PMU access
  436. */
  437. static u64 armv8pmu_pmcr_read(void)
  438. {
  439. return read_pmcr();
  440. }
  441. static void armv8pmu_pmcr_write(u64 val)
  442. {
  443. val &= ARMV8_PMU_PMCR_MASK;
  444. isb();
  445. write_pmcr(val);
  446. }
  447. static int armv8pmu_has_overflowed(u64 pmovsr)
  448. {
  449. return !!(pmovsr & ARMV8_PMU_OVERFLOWED_MASK);
  450. }
  451. static int armv8pmu_counter_has_overflowed(u64 pmnc, int idx)
  452. {
  453. return !!(pmnc & BIT(idx));
  454. }
  455. static u64 armv8pmu_read_evcntr(int idx)
  456. {
  457. return read_pmevcntrn(idx);
  458. }
  459. static u64 armv8pmu_read_hw_counter(struct perf_event *event)
  460. {
  461. int idx = event->hw.idx;
  462. u64 val = armv8pmu_read_evcntr(idx);
  463. if (armv8pmu_event_is_chained(event))
  464. val = (val << 32) | armv8pmu_read_evcntr(idx - 1);
  465. return val;
  466. }
  467. /*
  468. * The cycle counter is always a 64-bit counter. When ARMV8_PMU_PMCR_LP
  469. * is set the event counters also become 64-bit counters. Unless the
  470. * user has requested a long counter (attr.config1) then we want to
  471. * interrupt upon 32-bit overflow - we achieve this by applying a bias.
  472. */
  473. static bool armv8pmu_event_needs_bias(struct perf_event *event)
  474. {
  475. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  476. struct hw_perf_event *hwc = &event->hw;
  477. int idx = hwc->idx;
  478. if (armv8pmu_event_is_64bit(event))
  479. return false;
  480. if (armv8pmu_has_long_event(cpu_pmu) ||
  481. idx >= ARMV8_PMU_MAX_GENERAL_COUNTERS)
  482. return true;
  483. return false;
  484. }
  485. static u64 armv8pmu_bias_long_counter(struct perf_event *event, u64 value)
  486. {
  487. if (armv8pmu_event_needs_bias(event))
  488. value |= GENMASK_ULL(63, 32);
  489. return value;
  490. }
  491. static u64 armv8pmu_unbias_long_counter(struct perf_event *event, u64 value)
  492. {
  493. if (armv8pmu_event_needs_bias(event))
  494. value &= ~GENMASK_ULL(63, 32);
  495. return value;
  496. }
  497. static u64 armv8pmu_read_counter(struct perf_event *event)
  498. {
  499. struct hw_perf_event *hwc = &event->hw;
  500. int idx = hwc->idx;
  501. u64 value;
  502. if (idx == ARMV8_PMU_CYCLE_IDX)
  503. value = read_pmccntr();
  504. else if (idx == ARMV8_PMU_INSTR_IDX)
  505. value = read_pmicntr();
  506. else
  507. value = armv8pmu_read_hw_counter(event);
  508. return armv8pmu_unbias_long_counter(event, value);
  509. }
  510. static void armv8pmu_write_evcntr(int idx, u64 value)
  511. {
  512. write_pmevcntrn(idx, value);
  513. }
  514. static void armv8pmu_write_hw_counter(struct perf_event *event,
  515. u64 value)
  516. {
  517. int idx = event->hw.idx;
  518. if (armv8pmu_event_is_chained(event)) {
  519. armv8pmu_write_evcntr(idx, upper_32_bits(value));
  520. armv8pmu_write_evcntr(idx - 1, lower_32_bits(value));
  521. } else {
  522. armv8pmu_write_evcntr(idx, value);
  523. }
  524. }
  525. static void armv8pmu_write_counter(struct perf_event *event, u64 value)
  526. {
  527. struct hw_perf_event *hwc = &event->hw;
  528. int idx = hwc->idx;
  529. value = armv8pmu_bias_long_counter(event, value);
  530. if (idx == ARMV8_PMU_CYCLE_IDX)
  531. write_pmccntr(value);
  532. else if (idx == ARMV8_PMU_INSTR_IDX)
  533. write_pmicntr(value);
  534. else
  535. armv8pmu_write_hw_counter(event, value);
  536. }
  537. static void armv8pmu_write_evtype(int idx, unsigned long val)
  538. {
  539. unsigned long mask = ARMV8_PMU_EVTYPE_EVENT |
  540. ARMV8_PMU_INCLUDE_EL2 |
  541. ARMV8_PMU_EXCLUDE_EL0 |
  542. ARMV8_PMU_EXCLUDE_EL1;
  543. if (IS_ENABLED(CONFIG_ARM64))
  544. mask |= ARMV8_PMU_EVTYPE_TC | ARMV8_PMU_EVTYPE_TH;
  545. val &= mask;
  546. write_pmevtypern(idx, val);
  547. }
  548. static void armv8pmu_write_event_type(struct perf_event *event)
  549. {
  550. struct hw_perf_event *hwc = &event->hw;
  551. int idx = hwc->idx;
  552. /*
  553. * For chained events, the low counter is programmed to count
  554. * the event of interest and the high counter is programmed
  555. * with CHAIN event code with filters set to count at all ELs.
  556. */
  557. if (armv8pmu_event_is_chained(event)) {
  558. u32 chain_evt = ARMV8_PMUV3_PERFCTR_CHAIN |
  559. ARMV8_PMU_INCLUDE_EL2;
  560. armv8pmu_write_evtype(idx - 1, hwc->config_base);
  561. armv8pmu_write_evtype(idx, chain_evt);
  562. } else {
  563. if (idx == ARMV8_PMU_CYCLE_IDX)
  564. write_pmccfiltr(hwc->config_base);
  565. else if (idx == ARMV8_PMU_INSTR_IDX)
  566. write_pmicfiltr(hwc->config_base);
  567. else
  568. armv8pmu_write_evtype(idx, hwc->config_base);
  569. }
  570. }
  571. static u64 armv8pmu_event_cnten_mask(struct perf_event *event)
  572. {
  573. int counter = event->hw.idx;
  574. u64 mask = BIT(counter);
  575. if (armv8pmu_event_is_chained(event))
  576. mask |= BIT(counter - 1);
  577. return mask;
  578. }
  579. static void armv8pmu_enable_counter(u64 mask)
  580. {
  581. /*
  582. * Make sure event configuration register writes are visible before we
  583. * enable the counter.
  584. * */
  585. isb();
  586. write_pmcntenset(mask);
  587. }
  588. static void armv8pmu_enable_event_counter(struct perf_event *event)
  589. {
  590. struct perf_event_attr *attr = &event->attr;
  591. u64 mask = armv8pmu_event_cnten_mask(event);
  592. kvm_set_pmu_events(mask, attr);
  593. /* We rely on the hypervisor switch code to enable guest counters */
  594. if (!kvm_pmu_counter_deferred(attr))
  595. armv8pmu_enable_counter(mask);
  596. }
  597. static void armv8pmu_disable_counter(u64 mask)
  598. {
  599. write_pmcntenclr(mask);
  600. /*
  601. * Make sure the effects of disabling the counter are visible before we
  602. * start configuring the event.
  603. */
  604. isb();
  605. }
  606. static void armv8pmu_disable_event_counter(struct perf_event *event)
  607. {
  608. struct perf_event_attr *attr = &event->attr;
  609. u64 mask = armv8pmu_event_cnten_mask(event);
  610. kvm_clr_pmu_events(mask);
  611. /* We rely on the hypervisor switch code to disable guest counters */
  612. if (!kvm_pmu_counter_deferred(attr))
  613. armv8pmu_disable_counter(mask);
  614. }
  615. static void armv8pmu_enable_intens(u64 mask)
  616. {
  617. write_pmintenset(mask);
  618. }
  619. static void armv8pmu_enable_event_irq(struct perf_event *event)
  620. {
  621. armv8pmu_enable_intens(BIT(event->hw.idx));
  622. }
  623. static void armv8pmu_disable_intens(u64 mask)
  624. {
  625. write_pmintenclr(mask);
  626. isb();
  627. /* Clear the overflow flag in case an interrupt is pending. */
  628. write_pmovsclr(mask);
  629. isb();
  630. }
  631. static void armv8pmu_disable_event_irq(struct perf_event *event)
  632. {
  633. armv8pmu_disable_intens(BIT(event->hw.idx));
  634. }
  635. static u64 armv8pmu_getreset_flags(void)
  636. {
  637. u64 value;
  638. /* Read */
  639. value = read_pmovsclr();
  640. /* Write to clear flags */
  641. value &= ARMV8_PMU_OVERFLOWED_MASK;
  642. write_pmovsclr(value);
  643. return value;
  644. }
  645. static void update_pmuserenr(u64 val)
  646. {
  647. lockdep_assert_irqs_disabled();
  648. /*
  649. * The current PMUSERENR_EL0 value might be the value for the guest.
  650. * If that's the case, have KVM keep tracking of the register value
  651. * for the host EL0 so that KVM can restore it before returning to
  652. * the host EL0. Otherwise, update the register now.
  653. */
  654. if (kvm_set_pmuserenr(val))
  655. return;
  656. write_pmuserenr(val);
  657. }
  658. static void armv8pmu_disable_user_access(void)
  659. {
  660. update_pmuserenr(0);
  661. }
  662. static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)
  663. {
  664. int i;
  665. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  666. if (is_pmuv3p9(cpu_pmu->pmuver)) {
  667. u64 mask = 0;
  668. for_each_set_bit(i, cpuc->used_mask, ARMPMU_MAX_HWEVENTS) {
  669. if (armv8pmu_event_has_user_read(cpuc->events[i]))
  670. mask |= BIT(i);
  671. }
  672. write_pmuacr(mask);
  673. } else {
  674. /* Clear any unused counters to avoid leaking their contents */
  675. for_each_andnot_bit(i, cpu_pmu->cntr_mask, cpuc->used_mask,
  676. ARMPMU_MAX_HWEVENTS) {
  677. if (i == ARMV8_PMU_CYCLE_IDX)
  678. write_pmccntr(0);
  679. else if (i == ARMV8_PMU_INSTR_IDX)
  680. write_pmicntr(0);
  681. else
  682. armv8pmu_write_evcntr(i, 0);
  683. }
  684. }
  685. update_pmuserenr(ARMV8_PMU_USERENR_ER | ARMV8_PMU_USERENR_CR | ARMV8_PMU_USERENR_UEN);
  686. }
  687. static void armv8pmu_enable_event(struct perf_event *event)
  688. {
  689. armv8pmu_write_event_type(event);
  690. armv8pmu_enable_event_irq(event);
  691. armv8pmu_enable_event_counter(event);
  692. }
  693. static void armv8pmu_disable_event(struct perf_event *event)
  694. {
  695. armv8pmu_disable_event_counter(event);
  696. armv8pmu_disable_event_irq(event);
  697. }
  698. static void armv8pmu_start(struct arm_pmu *cpu_pmu)
  699. {
  700. struct perf_event_context *ctx;
  701. struct pmu_hw_events *hw_events = this_cpu_ptr(cpu_pmu->hw_events);
  702. int nr_user = 0;
  703. ctx = perf_cpu_task_ctx();
  704. if (ctx)
  705. nr_user = ctx->nr_user;
  706. if (sysctl_perf_user_access && nr_user)
  707. armv8pmu_enable_user_access(cpu_pmu);
  708. else
  709. armv8pmu_disable_user_access();
  710. kvm_vcpu_pmu_resync_el0();
  711. if (hw_events->branch_users)
  712. brbe_enable(cpu_pmu);
  713. /* Enable all counters */
  714. armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
  715. }
  716. static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
  717. {
  718. struct pmu_hw_events *hw_events = this_cpu_ptr(cpu_pmu->hw_events);
  719. if (hw_events->branch_users)
  720. brbe_disable();
  721. /* Disable all counters */
  722. armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
  723. }
  724. static void read_branch_records(struct pmu_hw_events *cpuc,
  725. struct perf_event *event,
  726. struct perf_sample_data *data)
  727. {
  728. struct perf_branch_stack *branch_stack = cpuc->branch_stack;
  729. brbe_read_filtered_entries(branch_stack, event);
  730. perf_sample_save_brstack(data, event, branch_stack, NULL);
  731. }
  732. static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
  733. {
  734. u64 pmovsr;
  735. struct perf_sample_data data;
  736. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  737. struct pt_regs *regs;
  738. int idx;
  739. /*
  740. * Get and reset the IRQ flags
  741. */
  742. pmovsr = armv8pmu_getreset_flags();
  743. /*
  744. * Did an overflow occur?
  745. */
  746. if (!armv8pmu_has_overflowed(pmovsr))
  747. return IRQ_NONE;
  748. /*
  749. * Handle the counter(s) overflow(s)
  750. */
  751. regs = get_irq_regs();
  752. /*
  753. * Stop the PMU while processing the counter overflows
  754. * to prevent skews in group events.
  755. */
  756. armv8pmu_stop(cpu_pmu);
  757. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
  758. struct perf_event *event = cpuc->events[idx];
  759. struct hw_perf_event *hwc;
  760. /* Ignore if we don't have an event. */
  761. if (!event)
  762. continue;
  763. /*
  764. * We have a single interrupt for all counters. Check that
  765. * each counter has overflowed before we process it.
  766. */
  767. if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
  768. continue;
  769. hwc = &event->hw;
  770. armpmu_event_update(event);
  771. perf_sample_data_init(&data, 0, hwc->last_period);
  772. if (!armpmu_event_set_period(event))
  773. continue;
  774. if (has_branch_stack(event))
  775. read_branch_records(cpuc, event, &data);
  776. /*
  777. * Perf event overflow will queue the processing of the event as
  778. * an irq_work which will be taken care of in the handling of
  779. * IPI_IRQ_WORK.
  780. */
  781. perf_event_overflow(event, &data, regs);
  782. }
  783. armv8pmu_start(cpu_pmu);
  784. return IRQ_HANDLED;
  785. }
  786. static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
  787. struct arm_pmu *cpu_pmu)
  788. {
  789. int idx;
  790. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
  791. if (!test_and_set_bit(idx, cpuc->used_mask))
  792. return idx;
  793. }
  794. return -EAGAIN;
  795. }
  796. static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
  797. struct arm_pmu *cpu_pmu)
  798. {
  799. int idx;
  800. /*
  801. * Chaining requires two consecutive event counters, where
  802. * the lower idx must be even.
  803. */
  804. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
  805. if (!(idx & 0x1))
  806. continue;
  807. if (!test_and_set_bit(idx, cpuc->used_mask)) {
  808. /* Check if the preceding even counter is available */
  809. if (!test_and_set_bit(idx - 1, cpuc->used_mask))
  810. return idx;
  811. /* Release the Odd counter */
  812. clear_bit(idx, cpuc->used_mask);
  813. }
  814. }
  815. return -EAGAIN;
  816. }
  817. static bool armv8pmu_can_use_pmccntr(struct pmu_hw_events *cpuc,
  818. struct perf_event *event)
  819. {
  820. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  821. struct hw_perf_event *hwc = &event->hw;
  822. unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
  823. if (evtype != ARMV8_PMUV3_PERFCTR_CPU_CYCLES)
  824. return false;
  825. /*
  826. * A CPU_CYCLES event with threshold counting cannot use PMCCNTR_EL0
  827. * since it lacks threshold support.
  828. */
  829. if (armv8pmu_event_get_threshold(&event->attr))
  830. return false;
  831. /*
  832. * PMCCNTR_EL0 is not affected by BRBE controls like BRBCR_ELx.FZP.
  833. * So don't use it for branch events.
  834. */
  835. if (has_branch_stack(event))
  836. return false;
  837. /*
  838. * The PMCCNTR_EL0 increments from the processor clock rather than
  839. * the PE clock (ARM DDI0487 L.b D13.1.3) which means it'll continue
  840. * counting on a WFI PE if one of its SMT sibling is not idle on a
  841. * multi-threaded implementation. So don't use it on SMT cores.
  842. */
  843. if (cpu_pmu->has_smt)
  844. return false;
  845. return true;
  846. }
  847. static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
  848. struct perf_event *event)
  849. {
  850. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  851. struct hw_perf_event *hwc = &event->hw;
  852. unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
  853. /* Always prefer to place a cycle counter into the cycle counter. */
  854. if (armv8pmu_can_use_pmccntr(cpuc, event)) {
  855. if (!test_and_set_bit(ARMV8_PMU_CYCLE_IDX, cpuc->used_mask))
  856. return ARMV8_PMU_CYCLE_IDX;
  857. else if (armv8pmu_event_is_64bit(event) &&
  858. armv8pmu_event_want_user_access(event) &&
  859. !armv8pmu_has_long_event(cpu_pmu))
  860. return -EAGAIN;
  861. }
  862. /*
  863. * Always prefer to place a instruction counter into the instruction counter,
  864. * but don't expose the instruction counter to userspace access as userspace
  865. * may not know how to handle it.
  866. */
  867. if ((evtype == ARMV8_PMUV3_PERFCTR_INST_RETIRED) &&
  868. !armv8pmu_event_get_threshold(&event->attr) &&
  869. test_bit(ARMV8_PMU_INSTR_IDX, cpu_pmu->cntr_mask) &&
  870. !armv8pmu_event_want_user_access(event)) {
  871. if (!test_and_set_bit(ARMV8_PMU_INSTR_IDX, cpuc->used_mask))
  872. return ARMV8_PMU_INSTR_IDX;
  873. }
  874. /*
  875. * Otherwise use events counters
  876. */
  877. if (armv8pmu_event_is_chained(event))
  878. return armv8pmu_get_chain_idx(cpuc, cpu_pmu);
  879. else
  880. return armv8pmu_get_single_idx(cpuc, cpu_pmu);
  881. }
  882. static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  883. struct perf_event *event)
  884. {
  885. int idx = event->hw.idx;
  886. clear_bit(idx, cpuc->used_mask);
  887. if (armv8pmu_event_is_chained(event))
  888. clear_bit(idx - 1, cpuc->used_mask);
  889. }
  890. static int armv8pmu_user_event_idx(struct perf_event *event)
  891. {
  892. if (!sysctl_perf_user_access || !armv8pmu_event_has_user_read(event))
  893. return 0;
  894. return event->hw.idx + 1;
  895. }
  896. static void armv8pmu_sched_task(struct perf_event_pmu_context *pmu_ctx,
  897. struct task_struct *task, bool sched_in)
  898. {
  899. struct arm_pmu *armpmu = to_arm_pmu(pmu_ctx->pmu);
  900. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  901. if (!hw_events->branch_users)
  902. return;
  903. if (sched_in)
  904. brbe_invalidate();
  905. }
  906. /*
  907. * Add an event filter to a given event.
  908. */
  909. static int armv8pmu_set_event_filter(struct hw_perf_event *event,
  910. struct perf_event_attr *attr)
  911. {
  912. unsigned long config_base = 0;
  913. struct perf_event *perf_event = container_of(attr, struct perf_event,
  914. attr);
  915. struct arm_pmu *cpu_pmu = to_arm_pmu(perf_event->pmu);
  916. u32 th;
  917. if (attr->exclude_idle) {
  918. pr_debug("ARM performance counters do not support mode exclusion\n");
  919. return -EOPNOTSUPP;
  920. }
  921. if (has_branch_stack(perf_event)) {
  922. if (!brbe_num_branch_records(cpu_pmu) || !brbe_branch_attr_valid(perf_event))
  923. return -EOPNOTSUPP;
  924. perf_event->attach_state |= PERF_ATTACH_SCHED_CB;
  925. }
  926. /*
  927. * If we're running in hyp mode, then we *are* the hypervisor.
  928. * Therefore we ignore exclude_hv in this configuration, since
  929. * there's no hypervisor to sample anyway. This is consistent
  930. * with other architectures (x86 and Power).
  931. */
  932. if (is_kernel_in_hyp_mode()) {
  933. if (!attr->exclude_kernel && !attr->exclude_host)
  934. config_base |= ARMV8_PMU_INCLUDE_EL2;
  935. if (attr->exclude_guest)
  936. config_base |= ARMV8_PMU_EXCLUDE_EL1;
  937. if (attr->exclude_host)
  938. config_base |= ARMV8_PMU_EXCLUDE_EL0;
  939. } else {
  940. if (!attr->exclude_hv && !attr->exclude_host)
  941. config_base |= ARMV8_PMU_INCLUDE_EL2;
  942. }
  943. /*
  944. * Filter out !VHE kernels and guest kernels
  945. */
  946. if (attr->exclude_kernel)
  947. config_base |= ARMV8_PMU_EXCLUDE_EL1;
  948. if (attr->exclude_user)
  949. config_base |= ARMV8_PMU_EXCLUDE_EL0;
  950. /*
  951. * If FEAT_PMUv3_TH isn't implemented, then THWIDTH (threshold_max) will
  952. * be 0 and will also trigger this check, preventing it from being used.
  953. */
  954. th = armv8pmu_event_get_threshold(attr);
  955. if (th > threshold_max(cpu_pmu)) {
  956. pr_debug("PMU event threshold exceeds max value\n");
  957. return -EINVAL;
  958. }
  959. if (th) {
  960. config_base |= FIELD_PREP(ARMV8_PMU_EVTYPE_TH, th);
  961. config_base |= FIELD_PREP(ARMV8_PMU_EVTYPE_TC,
  962. armv8pmu_event_threshold_control(attr));
  963. }
  964. /*
  965. * Install the filter into config_base as this is used to
  966. * construct the event type.
  967. */
  968. event->config_base = config_base;
  969. return 0;
  970. }
  971. static void armv8pmu_reset(void *info)
  972. {
  973. struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
  974. u64 pmcr, mask;
  975. bitmap_to_arr64(&mask, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS);
  976. /* The counter and interrupt enable registers are unknown at reset. */
  977. armv8pmu_disable_counter(mask);
  978. armv8pmu_disable_intens(mask);
  979. /* Clear the counters we flip at guest entry/exit */
  980. kvm_clr_pmu_events(mask);
  981. if (brbe_num_branch_records(cpu_pmu)) {
  982. brbe_disable();
  983. brbe_invalidate();
  984. }
  985. /*
  986. * Initialize & Reset PMNC. Request overflow interrupt for
  987. * 64 bit cycle counter but cheat in armv8pmu_write_counter().
  988. */
  989. pmcr = ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_LC;
  990. /* Enable long event counter support where available */
  991. if (armv8pmu_has_long_event(cpu_pmu))
  992. pmcr |= ARMV8_PMU_PMCR_LP;
  993. armv8pmu_pmcr_write(pmcr);
  994. }
  995. static int __armv8_pmuv3_map_event_id(struct arm_pmu *armpmu,
  996. struct perf_event *event)
  997. {
  998. if (event->attr.type == PERF_TYPE_HARDWARE &&
  999. event->attr.config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) {
  1000. if (test_bit(ARMV8_PMUV3_PERFCTR_BR_RETIRED,
  1001. armpmu->pmceid_bitmap))
  1002. return ARMV8_PMUV3_PERFCTR_BR_RETIRED;
  1003. if (test_bit(ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
  1004. armpmu->pmceid_bitmap))
  1005. return ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED;
  1006. return HW_OP_UNSUPPORTED;
  1007. }
  1008. return armpmu_map_event(event, &armv8_pmuv3_perf_map,
  1009. &armv8_pmuv3_perf_cache_map,
  1010. ARMV8_PMU_EVTYPE_EVENT);
  1011. }
  1012. static int __armv8_pmuv3_map_event(struct perf_event *event,
  1013. const unsigned (*extra_event_map)
  1014. [PERF_COUNT_HW_MAX],
  1015. const unsigned (*extra_cache_map)
  1016. [PERF_COUNT_HW_CACHE_MAX]
  1017. [PERF_COUNT_HW_CACHE_OP_MAX]
  1018. [PERF_COUNT_HW_CACHE_RESULT_MAX])
  1019. {
  1020. int hw_event_id;
  1021. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  1022. hw_event_id = __armv8_pmuv3_map_event_id(armpmu, event);
  1023. /*
  1024. * CHAIN events only work when paired with an adjacent counter, and it
  1025. * never makes sense for a user to open one in isolation, as they'll be
  1026. * rotated arbitrarily.
  1027. */
  1028. if (hw_event_id == ARMV8_PMUV3_PERFCTR_CHAIN)
  1029. return -EINVAL;
  1030. if (armv8pmu_event_is_64bit(event))
  1031. event->hw.flags |= ARMPMU_EVT_64BIT;
  1032. /*
  1033. * User events must be allocated into a single counter, and so
  1034. * must not be chained.
  1035. *
  1036. * Most 64-bit events require long counter support, but 64-bit
  1037. * CPU_CYCLES events can be placed into the dedicated cycle
  1038. * counter when this is free.
  1039. */
  1040. if (armv8pmu_event_want_user_access(event)) {
  1041. if (!(event->attach_state & PERF_ATTACH_TASK))
  1042. return -EINVAL;
  1043. if (armv8pmu_event_is_64bit(event) &&
  1044. (hw_event_id != ARMV8_PMUV3_PERFCTR_CPU_CYCLES) &&
  1045. !armv8pmu_has_long_event(armpmu))
  1046. return -EOPNOTSUPP;
  1047. event->hw.flags |= PERF_EVENT_FLAG_USER_READ_CNT;
  1048. }
  1049. /* Only expose micro/arch events supported by this PMU */
  1050. if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
  1051. && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
  1052. return hw_event_id;
  1053. }
  1054. return armpmu_map_event(event, extra_event_map, extra_cache_map,
  1055. ARMV8_PMU_EVTYPE_EVENT);
  1056. }
  1057. static int armv8_pmuv3_map_event(struct perf_event *event)
  1058. {
  1059. return __armv8_pmuv3_map_event(event, NULL, NULL);
  1060. }
  1061. static int armv8_a53_map_event(struct perf_event *event)
  1062. {
  1063. return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
  1064. }
  1065. static int armv8_a57_map_event(struct perf_event *event)
  1066. {
  1067. return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
  1068. }
  1069. static int armv8_a73_map_event(struct perf_event *event)
  1070. {
  1071. return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
  1072. }
  1073. static int armv8_thunder_map_event(struct perf_event *event)
  1074. {
  1075. return __armv8_pmuv3_map_event(event, NULL,
  1076. &armv8_thunder_perf_cache_map);
  1077. }
  1078. static int armv8_vulcan_map_event(struct perf_event *event)
  1079. {
  1080. return __armv8_pmuv3_map_event(event, NULL,
  1081. &armv8_vulcan_perf_cache_map);
  1082. }
  1083. struct armv8pmu_probe_info {
  1084. struct arm_pmu *pmu;
  1085. bool present;
  1086. };
  1087. static void __armv8pmu_probe_pmu(void *info)
  1088. {
  1089. struct armv8pmu_probe_info *probe = info;
  1090. struct arm_pmu *cpu_pmu = probe->pmu;
  1091. u64 pmceid_raw[2];
  1092. u32 pmceid[2];
  1093. int pmuver;
  1094. pmuver = read_pmuver();
  1095. if (!pmuv3_implemented(pmuver))
  1096. return;
  1097. cpu_pmu->pmuver = pmuver;
  1098. probe->present = true;
  1099. /* Read the nb of CNTx counters supported from PMNC */
  1100. bitmap_set(cpu_pmu->cntr_mask,
  1101. 0, FIELD_GET(ARMV8_PMU_PMCR_N, armv8pmu_pmcr_read()));
  1102. /* Add the CPU cycles counter */
  1103. set_bit(ARMV8_PMU_CYCLE_IDX, cpu_pmu->cntr_mask);
  1104. /* Add the CPU instructions counter */
  1105. if (pmuv3_has_icntr())
  1106. set_bit(ARMV8_PMU_INSTR_IDX, cpu_pmu->cntr_mask);
  1107. pmceid[0] = pmceid_raw[0] = read_pmceid0();
  1108. pmceid[1] = pmceid_raw[1] = read_pmceid1();
  1109. bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
  1110. pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
  1111. pmceid[0] = pmceid_raw[0] >> 32;
  1112. pmceid[1] = pmceid_raw[1] >> 32;
  1113. bitmap_from_arr32(cpu_pmu->pmceid_ext_bitmap,
  1114. pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
  1115. /* store PMMIR register for sysfs */
  1116. if (is_pmuv3p4(pmuver))
  1117. cpu_pmu->reg_pmmir = read_pmmir();
  1118. else
  1119. cpu_pmu->reg_pmmir = 0;
  1120. brbe_probe(cpu_pmu);
  1121. }
  1122. static int branch_records_alloc(struct arm_pmu *armpmu)
  1123. {
  1124. size_t size = struct_size_t(struct perf_branch_stack, entries,
  1125. brbe_num_branch_records(armpmu));
  1126. int cpu;
  1127. for_each_cpu(cpu, &armpmu->supported_cpus) {
  1128. struct pmu_hw_events *events_cpu;
  1129. events_cpu = per_cpu_ptr(armpmu->hw_events, cpu);
  1130. events_cpu->branch_stack = kmalloc(size, GFP_KERNEL);
  1131. if (!events_cpu->branch_stack)
  1132. return -ENOMEM;
  1133. }
  1134. return 0;
  1135. }
  1136. static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
  1137. {
  1138. struct armv8pmu_probe_info probe = {
  1139. .pmu = cpu_pmu,
  1140. .present = false,
  1141. };
  1142. int ret;
  1143. ret = smp_call_function_any(&cpu_pmu->supported_cpus,
  1144. __armv8pmu_probe_pmu,
  1145. &probe, 1);
  1146. if (ret)
  1147. return ret;
  1148. if (!probe.present)
  1149. return -ENODEV;
  1150. if (brbe_num_branch_records(cpu_pmu)) {
  1151. ret = branch_records_alloc(cpu_pmu);
  1152. if (ret)
  1153. return ret;
  1154. }
  1155. return 0;
  1156. }
  1157. static void armv8pmu_disable_user_access_ipi(void *unused)
  1158. {
  1159. armv8pmu_disable_user_access();
  1160. }
  1161. static int armv8pmu_proc_user_access_handler(const struct ctl_table *table, int write,
  1162. void *buffer, size_t *lenp, loff_t *ppos)
  1163. {
  1164. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  1165. if (ret || !write || sysctl_perf_user_access)
  1166. return ret;
  1167. on_each_cpu(armv8pmu_disable_user_access_ipi, NULL, 1);
  1168. return 0;
  1169. }
  1170. static const struct ctl_table armv8_pmu_sysctl_table[] = {
  1171. {
  1172. .procname = "perf_user_access",
  1173. .data = &sysctl_perf_user_access,
  1174. .maxlen = sizeof(unsigned int),
  1175. .mode = 0644,
  1176. .proc_handler = armv8pmu_proc_user_access_handler,
  1177. .extra1 = SYSCTL_ZERO,
  1178. .extra2 = SYSCTL_ONE,
  1179. },
  1180. };
  1181. static void armv8_pmu_register_sysctl_table(void)
  1182. {
  1183. static u32 tbl_registered = 0;
  1184. if (!cmpxchg_relaxed(&tbl_registered, 0, 1))
  1185. register_sysctl("kernel", armv8_pmu_sysctl_table);
  1186. }
  1187. static int armv8_pmu_init(struct arm_pmu *cpu_pmu, char *name,
  1188. int (*map_event)(struct perf_event *event))
  1189. {
  1190. int ret = armv8pmu_probe_pmu(cpu_pmu);
  1191. if (ret)
  1192. return ret;
  1193. cpu_pmu->handle_irq = armv8pmu_handle_irq;
  1194. cpu_pmu->enable = armv8pmu_enable_event;
  1195. cpu_pmu->disable = armv8pmu_disable_event;
  1196. cpu_pmu->read_counter = armv8pmu_read_counter;
  1197. cpu_pmu->write_counter = armv8pmu_write_counter;
  1198. cpu_pmu->get_event_idx = armv8pmu_get_event_idx;
  1199. cpu_pmu->clear_event_idx = armv8pmu_clear_event_idx;
  1200. cpu_pmu->start = armv8pmu_start;
  1201. cpu_pmu->stop = armv8pmu_stop;
  1202. cpu_pmu->reset = armv8pmu_reset;
  1203. cpu_pmu->set_event_filter = armv8pmu_set_event_filter;
  1204. cpu_pmu->pmu.event_idx = armv8pmu_user_event_idx;
  1205. if (brbe_num_branch_records(cpu_pmu))
  1206. cpu_pmu->pmu.sched_task = armv8pmu_sched_task;
  1207. cpu_pmu->name = name;
  1208. cpu_pmu->map_event = map_event;
  1209. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] = &armv8_pmuv3_events_attr_group;
  1210. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] = &armv8_pmuv3_format_attr_group;
  1211. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_CAPS] = &armv8_pmuv3_caps_attr_group;
  1212. armv8_pmu_register_sysctl_table();
  1213. return 0;
  1214. }
  1215. #define PMUV3_INIT_SIMPLE(name) \
  1216. static int name##_pmu_init(struct arm_pmu *cpu_pmu) \
  1217. { \
  1218. return armv8_pmu_init(cpu_pmu, #name, armv8_pmuv3_map_event); \
  1219. }
  1220. #define PMUV3_INIT_MAP_EVENT(name, map_event) \
  1221. static int name##_pmu_init(struct arm_pmu *cpu_pmu) \
  1222. { \
  1223. return armv8_pmu_init(cpu_pmu, #name, map_event); \
  1224. }
  1225. PMUV3_INIT_SIMPLE(armv8_pmuv3)
  1226. PMUV3_INIT_SIMPLE(armv8_c1_nano)
  1227. PMUV3_INIT_SIMPLE(armv8_c1_premium)
  1228. PMUV3_INIT_SIMPLE(armv8_c1_pro)
  1229. PMUV3_INIT_SIMPLE(armv8_c1_ultra)
  1230. PMUV3_INIT_SIMPLE(armv8_cortex_a34)
  1231. PMUV3_INIT_SIMPLE(armv8_cortex_a55)
  1232. PMUV3_INIT_SIMPLE(armv8_cortex_a65)
  1233. PMUV3_INIT_SIMPLE(armv8_cortex_a75)
  1234. PMUV3_INIT_SIMPLE(armv8_cortex_a76)
  1235. PMUV3_INIT_SIMPLE(armv8_cortex_a77)
  1236. PMUV3_INIT_SIMPLE(armv8_cortex_a78)
  1237. PMUV3_INIT_SIMPLE(armv9_cortex_a320)
  1238. PMUV3_INIT_SIMPLE(armv9_cortex_a510)
  1239. PMUV3_INIT_SIMPLE(armv9_cortex_a520)
  1240. PMUV3_INIT_SIMPLE(armv9_cortex_a520ae)
  1241. PMUV3_INIT_SIMPLE(armv9_cortex_a710)
  1242. PMUV3_INIT_SIMPLE(armv9_cortex_a715)
  1243. PMUV3_INIT_SIMPLE(armv9_cortex_a720)
  1244. PMUV3_INIT_SIMPLE(armv9_cortex_a720ae)
  1245. PMUV3_INIT_SIMPLE(armv9_cortex_a725)
  1246. PMUV3_INIT_SIMPLE(armv8_cortex_x1)
  1247. PMUV3_INIT_SIMPLE(armv9_cortex_x2)
  1248. PMUV3_INIT_SIMPLE(armv9_cortex_x3)
  1249. PMUV3_INIT_SIMPLE(armv9_cortex_x4)
  1250. PMUV3_INIT_SIMPLE(armv9_cortex_x925)
  1251. PMUV3_INIT_SIMPLE(armv8_neoverse_e1)
  1252. PMUV3_INIT_SIMPLE(armv8_neoverse_n1)
  1253. PMUV3_INIT_SIMPLE(armv9_neoverse_n2)
  1254. PMUV3_INIT_SIMPLE(armv9_neoverse_n3)
  1255. PMUV3_INIT_SIMPLE(armv8_neoverse_v1)
  1256. PMUV3_INIT_SIMPLE(armv8_neoverse_v2)
  1257. PMUV3_INIT_SIMPLE(armv8_neoverse_v3)
  1258. PMUV3_INIT_SIMPLE(armv8_neoverse_v3ae)
  1259. PMUV3_INIT_SIMPLE(armv8_rainier)
  1260. PMUV3_INIT_SIMPLE(armv8_nvidia_carmel)
  1261. PMUV3_INIT_SIMPLE(armv8_nvidia_denver)
  1262. PMUV3_INIT_SIMPLE(armv8_samsung_mongoose)
  1263. PMUV3_INIT_MAP_EVENT(armv8_cortex_a35, armv8_a53_map_event)
  1264. PMUV3_INIT_MAP_EVENT(armv8_cortex_a53, armv8_a53_map_event)
  1265. PMUV3_INIT_MAP_EVENT(armv8_cortex_a57, armv8_a57_map_event)
  1266. PMUV3_INIT_MAP_EVENT(armv8_cortex_a72, armv8_a57_map_event)
  1267. PMUV3_INIT_MAP_EVENT(armv8_cortex_a73, armv8_a73_map_event)
  1268. PMUV3_INIT_MAP_EVENT(armv8_cavium_thunder, armv8_thunder_map_event)
  1269. PMUV3_INIT_MAP_EVENT(armv8_brcm_vulcan, armv8_vulcan_map_event)
  1270. static const struct of_device_id armv8_pmu_of_device_ids[] = {
  1271. {.compatible = "arm,armv8-pmuv3", .data = armv8_pmuv3_pmu_init},
  1272. {.compatible = "arm,c1-nano-pmu", .data = armv8_c1_nano_pmu_init},
  1273. {.compatible = "arm,c1-premium-pmu", .data = armv8_c1_premium_pmu_init},
  1274. {.compatible = "arm,c1-pro-pmu", .data = armv8_c1_pro_pmu_init},
  1275. {.compatible = "arm,c1-ultra-pmu", .data = armv8_c1_ultra_pmu_init},
  1276. {.compatible = "arm,cortex-a34-pmu", .data = armv8_cortex_a34_pmu_init},
  1277. {.compatible = "arm,cortex-a35-pmu", .data = armv8_cortex_a35_pmu_init},
  1278. {.compatible = "arm,cortex-a53-pmu", .data = armv8_cortex_a53_pmu_init},
  1279. {.compatible = "arm,cortex-a55-pmu", .data = armv8_cortex_a55_pmu_init},
  1280. {.compatible = "arm,cortex-a57-pmu", .data = armv8_cortex_a57_pmu_init},
  1281. {.compatible = "arm,cortex-a65-pmu", .data = armv8_cortex_a65_pmu_init},
  1282. {.compatible = "arm,cortex-a72-pmu", .data = armv8_cortex_a72_pmu_init},
  1283. {.compatible = "arm,cortex-a73-pmu", .data = armv8_cortex_a73_pmu_init},
  1284. {.compatible = "arm,cortex-a75-pmu", .data = armv8_cortex_a75_pmu_init},
  1285. {.compatible = "arm,cortex-a76-pmu", .data = armv8_cortex_a76_pmu_init},
  1286. {.compatible = "arm,cortex-a77-pmu", .data = armv8_cortex_a77_pmu_init},
  1287. {.compatible = "arm,cortex-a78-pmu", .data = armv8_cortex_a78_pmu_init},
  1288. {.compatible = "arm,cortex-a320-pmu", .data = armv9_cortex_a320_pmu_init},
  1289. {.compatible = "arm,cortex-a510-pmu", .data = armv9_cortex_a510_pmu_init},
  1290. {.compatible = "arm,cortex-a520-pmu", .data = armv9_cortex_a520_pmu_init},
  1291. {.compatible = "arm,cortex-a520ae-pmu", .data = armv9_cortex_a520ae_pmu_init},
  1292. {.compatible = "arm,cortex-a710-pmu", .data = armv9_cortex_a710_pmu_init},
  1293. {.compatible = "arm,cortex-a715-pmu", .data = armv9_cortex_a715_pmu_init},
  1294. {.compatible = "arm,cortex-a720-pmu", .data = armv9_cortex_a720_pmu_init},
  1295. {.compatible = "arm,cortex-a720ae-pmu", .data = armv9_cortex_a720ae_pmu_init},
  1296. {.compatible = "arm,cortex-a725-pmu", .data = armv9_cortex_a725_pmu_init},
  1297. {.compatible = "arm,cortex-x1-pmu", .data = armv8_cortex_x1_pmu_init},
  1298. {.compatible = "arm,cortex-x2-pmu", .data = armv9_cortex_x2_pmu_init},
  1299. {.compatible = "arm,cortex-x3-pmu", .data = armv9_cortex_x3_pmu_init},
  1300. {.compatible = "arm,cortex-x4-pmu", .data = armv9_cortex_x4_pmu_init},
  1301. {.compatible = "arm,cortex-x925-pmu", .data = armv9_cortex_x925_pmu_init},
  1302. {.compatible = "arm,neoverse-e1-pmu", .data = armv8_neoverse_e1_pmu_init},
  1303. {.compatible = "arm,neoverse-n1-pmu", .data = armv8_neoverse_n1_pmu_init},
  1304. {.compatible = "arm,neoverse-n2-pmu", .data = armv9_neoverse_n2_pmu_init},
  1305. {.compatible = "arm,neoverse-n3-pmu", .data = armv9_neoverse_n3_pmu_init},
  1306. {.compatible = "arm,neoverse-v1-pmu", .data = armv8_neoverse_v1_pmu_init},
  1307. {.compatible = "arm,neoverse-v2-pmu", .data = armv8_neoverse_v2_pmu_init},
  1308. {.compatible = "arm,neoverse-v3-pmu", .data = armv8_neoverse_v3_pmu_init},
  1309. {.compatible = "arm,neoverse-v3ae-pmu", .data = armv8_neoverse_v3ae_pmu_init},
  1310. {.compatible = "arm,rainier-pmu", .data = armv8_rainier_pmu_init},
  1311. {.compatible = "cavium,thunder-pmu", .data = armv8_cavium_thunder_pmu_init},
  1312. {.compatible = "brcm,vulcan-pmu", .data = armv8_brcm_vulcan_pmu_init},
  1313. {.compatible = "nvidia,carmel-pmu", .data = armv8_nvidia_carmel_pmu_init},
  1314. {.compatible = "nvidia,denver-pmu", .data = armv8_nvidia_denver_pmu_init},
  1315. {.compatible = "samsung,mongoose-pmu", .data = armv8_samsung_mongoose_pmu_init},
  1316. {},
  1317. };
  1318. static int armv8_pmu_device_probe(struct platform_device *pdev)
  1319. {
  1320. return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
  1321. }
  1322. static struct platform_driver armv8_pmu_driver = {
  1323. .driver = {
  1324. .name = ARMV8_PMU_PDEV_NAME,
  1325. .of_match_table = armv8_pmu_of_device_ids,
  1326. .suppress_bind_attrs = true,
  1327. },
  1328. .probe = armv8_pmu_device_probe,
  1329. };
  1330. static int __init armv8_pmu_driver_init(void)
  1331. {
  1332. int ret;
  1333. if (acpi_disabled)
  1334. ret = platform_driver_register(&armv8_pmu_driver);
  1335. else
  1336. ret = arm_pmu_acpi_probe(armv8_pmuv3_pmu_init);
  1337. if (!ret)
  1338. lockup_detector_retry_init();
  1339. return ret;
  1340. }
  1341. device_initcall(armv8_pmu_driver_init)
  1342. void arch_perf_update_userpage(struct perf_event *event,
  1343. struct perf_event_mmap_page *userpg, u64 now)
  1344. {
  1345. struct clock_read_data *rd;
  1346. unsigned int seq;
  1347. u64 ns;
  1348. userpg->cap_user_time = 0;
  1349. userpg->cap_user_time_zero = 0;
  1350. userpg->cap_user_time_short = 0;
  1351. userpg->cap_user_rdpmc = armv8pmu_event_has_user_read(event);
  1352. if (userpg->cap_user_rdpmc) {
  1353. if (event->hw.flags & ARMPMU_EVT_64BIT)
  1354. userpg->pmc_width = 64;
  1355. else
  1356. userpg->pmc_width = 32;
  1357. }
  1358. do {
  1359. rd = sched_clock_read_begin(&seq);
  1360. if (rd->read_sched_clock != arch_timer_read_counter)
  1361. return;
  1362. userpg->time_mult = rd->mult;
  1363. userpg->time_shift = rd->shift;
  1364. userpg->time_zero = rd->epoch_ns;
  1365. userpg->time_cycles = rd->epoch_cyc;
  1366. userpg->time_mask = rd->sched_clock_mask;
  1367. /*
  1368. * Subtract the cycle base, such that software that
  1369. * doesn't know about cap_user_time_short still 'works'
  1370. * assuming no wraps.
  1371. */
  1372. ns = mul_u64_u32_shr(rd->epoch_cyc, rd->mult, rd->shift);
  1373. userpg->time_zero -= ns;
  1374. } while (sched_clock_read_retry(seq));
  1375. userpg->time_offset = userpg->time_zero - now;
  1376. /*
  1377. * time_shift is not expected to be greater than 31 due to
  1378. * the original published conversion algorithm shifting a
  1379. * 32-bit value (now specifies a 64-bit value) - refer
  1380. * perf_event_mmap_page documentation in perf_event.h.
  1381. */
  1382. if (userpg->time_shift == 32) {
  1383. userpg->time_shift = 31;
  1384. userpg->time_mult >>= 1;
  1385. }
  1386. /*
  1387. * Internal timekeeping for enabled/running/stopped times
  1388. * is always computed with the sched_clock.
  1389. */
  1390. userpg->cap_user_time = 1;
  1391. userpg->cap_user_time_zero = 1;
  1392. userpg->cap_user_time_short = 1;
  1393. }