perf_event.c 44 KB

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