arm_pmuv3.c 47 KB

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