perf_event_v7.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022
  1. /*
  2. * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
  3. *
  4. * ARMv7 support: Jean Pihet <jpihet@mvista.com>
  5. * 2010 (c) MontaVista Software, LLC.
  6. *
  7. * Copied from ARMv6 code, with the low level code inspired
  8. * by the ARMv7 Oprofile code.
  9. *
  10. * Cortex-A8 has up to 4 configurable performance counters and
  11. * a single cycle counter.
  12. * Cortex-A9 has up to 31 configurable performance counters and
  13. * a single cycle counter.
  14. *
  15. * All counters can be enabled/disabled and IRQ masked separately. The cycle
  16. * counter and all 4 performance counters together can be reset separately.
  17. */
  18. #ifdef CONFIG_CPU_V7
  19. #include <asm/cp15.h>
  20. #include <asm/cputype.h>
  21. #include <asm/irq_regs.h>
  22. #include <asm/vfp.h>
  23. #include "../vfp/vfpinstr.h"
  24. #include <linux/of.h>
  25. #include <linux/perf/arm_pmu.h>
  26. #include <linux/platform_device.h>
  27. /*
  28. * Common ARMv7 event types
  29. *
  30. * Note: An implementation may not be able to count all of these events
  31. * but the encodings are considered to be `reserved' in the case that
  32. * they are not available.
  33. */
  34. #define ARMV7_PERFCTR_PMNC_SW_INCR 0x00
  35. #define ARMV7_PERFCTR_L1_ICACHE_REFILL 0x01
  36. #define ARMV7_PERFCTR_ITLB_REFILL 0x02
  37. #define ARMV7_PERFCTR_L1_DCACHE_REFILL 0x03
  38. #define ARMV7_PERFCTR_L1_DCACHE_ACCESS 0x04
  39. #define ARMV7_PERFCTR_DTLB_REFILL 0x05
  40. #define ARMV7_PERFCTR_MEM_READ 0x06
  41. #define ARMV7_PERFCTR_MEM_WRITE 0x07
  42. #define ARMV7_PERFCTR_INSTR_EXECUTED 0x08
  43. #define ARMV7_PERFCTR_EXC_TAKEN 0x09
  44. #define ARMV7_PERFCTR_EXC_EXECUTED 0x0A
  45. #define ARMV7_PERFCTR_CID_WRITE 0x0B
  46. /*
  47. * ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
  48. * It counts:
  49. * - all (taken) branch instructions,
  50. * - instructions that explicitly write the PC,
  51. * - exception generating instructions.
  52. */
  53. #define ARMV7_PERFCTR_PC_WRITE 0x0C
  54. #define ARMV7_PERFCTR_PC_IMM_BRANCH 0x0D
  55. #define ARMV7_PERFCTR_PC_PROC_RETURN 0x0E
  56. #define ARMV7_PERFCTR_MEM_UNALIGNED_ACCESS 0x0F
  57. #define ARMV7_PERFCTR_PC_BRANCH_MIS_PRED 0x10
  58. #define ARMV7_PERFCTR_CLOCK_CYCLES 0x11
  59. #define ARMV7_PERFCTR_PC_BRANCH_PRED 0x12
  60. /* These events are defined by the PMUv2 supplement (ARM DDI 0457A). */
  61. #define ARMV7_PERFCTR_MEM_ACCESS 0x13
  62. #define ARMV7_PERFCTR_L1_ICACHE_ACCESS 0x14
  63. #define ARMV7_PERFCTR_L1_DCACHE_WB 0x15
  64. #define ARMV7_PERFCTR_L2_CACHE_ACCESS 0x16
  65. #define ARMV7_PERFCTR_L2_CACHE_REFILL 0x17
  66. #define ARMV7_PERFCTR_L2_CACHE_WB 0x18
  67. #define ARMV7_PERFCTR_BUS_ACCESS 0x19
  68. #define ARMV7_PERFCTR_MEM_ERROR 0x1A
  69. #define ARMV7_PERFCTR_INSTR_SPEC 0x1B
  70. #define ARMV7_PERFCTR_TTBR_WRITE 0x1C
  71. #define ARMV7_PERFCTR_BUS_CYCLES 0x1D
  72. #define ARMV7_PERFCTR_CPU_CYCLES 0xFF
  73. /* ARMv7 Cortex-A8 specific event types */
  74. #define ARMV7_A8_PERFCTR_L2_CACHE_ACCESS 0x43
  75. #define ARMV7_A8_PERFCTR_L2_CACHE_REFILL 0x44
  76. #define ARMV7_A8_PERFCTR_L1_ICACHE_ACCESS 0x50
  77. #define ARMV7_A8_PERFCTR_STALL_ISIDE 0x56
  78. /* ARMv7 Cortex-A9 specific event types */
  79. #define ARMV7_A9_PERFCTR_INSTR_CORE_RENAME 0x68
  80. #define ARMV7_A9_PERFCTR_STALL_ICACHE 0x60
  81. #define ARMV7_A9_PERFCTR_STALL_DISPATCH 0x66
  82. /* ARMv7 Cortex-A5 specific event types */
  83. #define ARMV7_A5_PERFCTR_PREFETCH_LINEFILL 0xc2
  84. #define ARMV7_A5_PERFCTR_PREFETCH_LINEFILL_DROP 0xc3
  85. /* ARMv7 Cortex-A15 specific event types */
  86. #define ARMV7_A15_PERFCTR_L1_DCACHE_ACCESS_READ 0x40
  87. #define ARMV7_A15_PERFCTR_L1_DCACHE_ACCESS_WRITE 0x41
  88. #define ARMV7_A15_PERFCTR_L1_DCACHE_REFILL_READ 0x42
  89. #define ARMV7_A15_PERFCTR_L1_DCACHE_REFILL_WRITE 0x43
  90. #define ARMV7_A15_PERFCTR_DTLB_REFILL_L1_READ 0x4C
  91. #define ARMV7_A15_PERFCTR_DTLB_REFILL_L1_WRITE 0x4D
  92. #define ARMV7_A15_PERFCTR_L2_CACHE_ACCESS_READ 0x50
  93. #define ARMV7_A15_PERFCTR_L2_CACHE_ACCESS_WRITE 0x51
  94. #define ARMV7_A15_PERFCTR_L2_CACHE_REFILL_READ 0x52
  95. #define ARMV7_A15_PERFCTR_L2_CACHE_REFILL_WRITE 0x53
  96. #define ARMV7_A15_PERFCTR_PC_WRITE_SPEC 0x76
  97. /* ARMv7 Cortex-A12 specific event types */
  98. #define ARMV7_A12_PERFCTR_L1_DCACHE_ACCESS_READ 0x40
  99. #define ARMV7_A12_PERFCTR_L1_DCACHE_ACCESS_WRITE 0x41
  100. #define ARMV7_A12_PERFCTR_L2_CACHE_ACCESS_READ 0x50
  101. #define ARMV7_A12_PERFCTR_L2_CACHE_ACCESS_WRITE 0x51
  102. #define ARMV7_A12_PERFCTR_PC_WRITE_SPEC 0x76
  103. #define ARMV7_A12_PERFCTR_PF_TLB_REFILL 0xe7
  104. /* ARMv7 Krait specific event types */
  105. #define KRAIT_PMRESR0_GROUP0 0xcc
  106. #define KRAIT_PMRESR1_GROUP0 0xd0
  107. #define KRAIT_PMRESR2_GROUP0 0xd4
  108. #define KRAIT_VPMRESR0_GROUP0 0xd8
  109. #define KRAIT_PERFCTR_L1_ICACHE_ACCESS 0x10011
  110. #define KRAIT_PERFCTR_L1_ICACHE_MISS 0x10010
  111. #define KRAIT_PERFCTR_L1_ITLB_ACCESS 0x12222
  112. #define KRAIT_PERFCTR_L1_DTLB_ACCESS 0x12210
  113. /* ARMv7 Scorpion specific event types */
  114. #define SCORPION_LPM0_GROUP0 0x4c
  115. #define SCORPION_LPM1_GROUP0 0x50
  116. #define SCORPION_LPM2_GROUP0 0x54
  117. #define SCORPION_L2LPM_GROUP0 0x58
  118. #define SCORPION_VLPM_GROUP0 0x5c
  119. #define SCORPION_ICACHE_ACCESS 0x10053
  120. #define SCORPION_ICACHE_MISS 0x10052
  121. #define SCORPION_DTLB_ACCESS 0x12013
  122. #define SCORPION_DTLB_MISS 0x12012
  123. #define SCORPION_ITLB_MISS 0x12021
  124. /*
  125. * Cortex-A8 HW events mapping
  126. *
  127. * The hardware events that we support. We do support cache operations but
  128. * we have harvard caches and no way to combine instruction and data
  129. * accesses/misses in hardware.
  130. */
  131. static const unsigned armv7_a8_perf_map[PERF_COUNT_HW_MAX] = {
  132. PERF_MAP_ALL_UNSUPPORTED,
  133. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  134. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  135. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  136. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  137. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  138. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  139. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV7_A8_PERFCTR_STALL_ISIDE,
  140. };
  141. static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  142. [PERF_COUNT_HW_CACHE_OP_MAX]
  143. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  144. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  145. /*
  146. * The performance counters don't differentiate between read and write
  147. * accesses/misses so this isn't strictly correct, but it's the best we
  148. * can do. Writes and reads get combined.
  149. */
  150. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  151. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  152. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  153. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  154. [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_A8_PERFCTR_L1_ICACHE_ACCESS,
  155. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_ICACHE_REFILL,
  156. [C(LL)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_A8_PERFCTR_L2_CACHE_ACCESS,
  157. [C(LL)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_A8_PERFCTR_L2_CACHE_REFILL,
  158. [C(LL)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_A8_PERFCTR_L2_CACHE_ACCESS,
  159. [C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_A8_PERFCTR_L2_CACHE_REFILL,
  160. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  161. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  162. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  163. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  164. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  165. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  166. [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  167. [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  168. };
  169. /*
  170. * Cortex-A9 HW events mapping
  171. */
  172. static const unsigned armv7_a9_perf_map[PERF_COUNT_HW_MAX] = {
  173. PERF_MAP_ALL_UNSUPPORTED,
  174. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  175. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_A9_PERFCTR_INSTR_CORE_RENAME,
  176. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  177. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  178. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  179. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  180. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV7_A9_PERFCTR_STALL_ICACHE,
  181. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV7_A9_PERFCTR_STALL_DISPATCH,
  182. };
  183. static const unsigned armv7_a9_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  184. [PERF_COUNT_HW_CACHE_OP_MAX]
  185. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  186. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  187. /*
  188. * The performance counters don't differentiate between read and write
  189. * accesses/misses so this isn't strictly correct, but it's the best we
  190. * can do. Writes and reads get combined.
  191. */
  192. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  193. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  194. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  195. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  196. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_ICACHE_REFILL,
  197. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  198. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  199. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  200. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  201. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  202. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  203. [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  204. [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  205. };
  206. /*
  207. * Cortex-A5 HW events mapping
  208. */
  209. static const unsigned armv7_a5_perf_map[PERF_COUNT_HW_MAX] = {
  210. PERF_MAP_ALL_UNSUPPORTED,
  211. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  212. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  213. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  214. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  215. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  216. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  217. };
  218. static const unsigned armv7_a5_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  219. [PERF_COUNT_HW_CACHE_OP_MAX]
  220. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  221. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  222. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  223. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  224. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  225. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  226. [C(L1D)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV7_A5_PERFCTR_PREFETCH_LINEFILL,
  227. [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV7_A5_PERFCTR_PREFETCH_LINEFILL_DROP,
  228. [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_ICACHE_ACCESS,
  229. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_ICACHE_REFILL,
  230. /*
  231. * The prefetch counters don't differentiate between the I side and the
  232. * D side.
  233. */
  234. [C(L1I)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV7_A5_PERFCTR_PREFETCH_LINEFILL,
  235. [C(L1I)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV7_A5_PERFCTR_PREFETCH_LINEFILL_DROP,
  236. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  237. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  238. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  239. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  240. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  241. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  242. [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  243. [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  244. };
  245. /*
  246. * Cortex-A15 HW events mapping
  247. */
  248. static const unsigned armv7_a15_perf_map[PERF_COUNT_HW_MAX] = {
  249. PERF_MAP_ALL_UNSUPPORTED,
  250. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  251. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  252. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  253. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  254. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_A15_PERFCTR_PC_WRITE_SPEC,
  255. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  256. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_BUS_CYCLES,
  257. };
  258. static const unsigned armv7_a15_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  259. [PERF_COUNT_HW_CACHE_OP_MAX]
  260. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  261. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  262. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_A15_PERFCTR_L1_DCACHE_ACCESS_READ,
  263. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_A15_PERFCTR_L1_DCACHE_REFILL_READ,
  264. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_A15_PERFCTR_L1_DCACHE_ACCESS_WRITE,
  265. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_A15_PERFCTR_L1_DCACHE_REFILL_WRITE,
  266. /*
  267. * Not all performance counters differentiate between read and write
  268. * accesses/misses so we're not always strictly correct, but it's the
  269. * best we can do. Writes and reads get combined in these cases.
  270. */
  271. [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_ICACHE_ACCESS,
  272. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_ICACHE_REFILL,
  273. [C(LL)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_A15_PERFCTR_L2_CACHE_ACCESS_READ,
  274. [C(LL)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_A15_PERFCTR_L2_CACHE_REFILL_READ,
  275. [C(LL)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_A15_PERFCTR_L2_CACHE_ACCESS_WRITE,
  276. [C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_A15_PERFCTR_L2_CACHE_REFILL_WRITE,
  277. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_A15_PERFCTR_DTLB_REFILL_L1_READ,
  278. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_A15_PERFCTR_DTLB_REFILL_L1_WRITE,
  279. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  280. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  281. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  282. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  283. [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  284. [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  285. };
  286. /*
  287. * Cortex-A7 HW events mapping
  288. */
  289. static const unsigned armv7_a7_perf_map[PERF_COUNT_HW_MAX] = {
  290. PERF_MAP_ALL_UNSUPPORTED,
  291. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  292. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  293. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  294. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  295. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  296. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  297. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_BUS_CYCLES,
  298. };
  299. static const unsigned armv7_a7_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  300. [PERF_COUNT_HW_CACHE_OP_MAX]
  301. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  302. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  303. /*
  304. * The performance counters don't differentiate between read and write
  305. * accesses/misses so this isn't strictly correct, but it's the best we
  306. * can do. Writes and reads get combined.
  307. */
  308. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  309. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  310. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  311. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  312. [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_ICACHE_ACCESS,
  313. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_ICACHE_REFILL,
  314. [C(LL)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_CACHE_ACCESS,
  315. [C(LL)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACHE_REFILL,
  316. [C(LL)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_CACHE_ACCESS,
  317. [C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACHE_REFILL,
  318. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  319. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  320. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  321. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  322. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  323. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  324. [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  325. [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  326. };
  327. /*
  328. * Cortex-A12 HW events mapping
  329. */
  330. static const unsigned armv7_a12_perf_map[PERF_COUNT_HW_MAX] = {
  331. PERF_MAP_ALL_UNSUPPORTED,
  332. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  333. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  334. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  335. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  336. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_A12_PERFCTR_PC_WRITE_SPEC,
  337. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  338. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_BUS_CYCLES,
  339. };
  340. static const unsigned armv7_a12_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  341. [PERF_COUNT_HW_CACHE_OP_MAX]
  342. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  343. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  344. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_A12_PERFCTR_L1_DCACHE_ACCESS_READ,
  345. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  346. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_A12_PERFCTR_L1_DCACHE_ACCESS_WRITE,
  347. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  348. /*
  349. * Not all performance counters differentiate between read and write
  350. * accesses/misses so we're not always strictly correct, but it's the
  351. * best we can do. Writes and reads get combined in these cases.
  352. */
  353. [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_ICACHE_ACCESS,
  354. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_ICACHE_REFILL,
  355. [C(LL)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_A12_PERFCTR_L2_CACHE_ACCESS_READ,
  356. [C(LL)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACHE_REFILL,
  357. [C(LL)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_A12_PERFCTR_L2_CACHE_ACCESS_WRITE,
  358. [C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACHE_REFILL,
  359. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  360. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  361. [C(DTLB)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV7_A12_PERFCTR_PF_TLB_REFILL,
  362. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  363. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_REFILL,
  364. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  365. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  366. [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  367. [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  368. };
  369. /*
  370. * Krait HW events mapping
  371. */
  372. static const unsigned krait_perf_map[PERF_COUNT_HW_MAX] = {
  373. PERF_MAP_ALL_UNSUPPORTED,
  374. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  375. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  376. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  377. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  378. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  379. };
  380. static const unsigned krait_perf_map_no_branch[PERF_COUNT_HW_MAX] = {
  381. PERF_MAP_ALL_UNSUPPORTED,
  382. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  383. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  384. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  385. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  386. };
  387. static const unsigned krait_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  388. [PERF_COUNT_HW_CACHE_OP_MAX]
  389. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  390. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  391. /*
  392. * The performance counters don't differentiate between read and write
  393. * accesses/misses so this isn't strictly correct, but it's the best we
  394. * can do. Writes and reads get combined.
  395. */
  396. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  397. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  398. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  399. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  400. [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = KRAIT_PERFCTR_L1_ICACHE_ACCESS,
  401. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = KRAIT_PERFCTR_L1_ICACHE_MISS,
  402. [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = KRAIT_PERFCTR_L1_DTLB_ACCESS,
  403. [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = KRAIT_PERFCTR_L1_DTLB_ACCESS,
  404. [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = KRAIT_PERFCTR_L1_ITLB_ACCESS,
  405. [C(ITLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = KRAIT_PERFCTR_L1_ITLB_ACCESS,
  406. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  407. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  408. [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  409. [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  410. };
  411. /*
  412. * Scorpion HW events mapping
  413. */
  414. static const unsigned scorpion_perf_map[PERF_COUNT_HW_MAX] = {
  415. PERF_MAP_ALL_UNSUPPORTED,
  416. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  417. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  418. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  419. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  420. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  421. };
  422. static const unsigned scorpion_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  423. [PERF_COUNT_HW_CACHE_OP_MAX]
  424. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  425. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  426. /*
  427. * The performance counters don't differentiate between read and write
  428. * accesses/misses so this isn't strictly correct, but it's the best we
  429. * can do. Writes and reads get combined.
  430. */
  431. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  432. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  433. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_DCACHE_ACCESS,
  434. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DCACHE_REFILL,
  435. [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = SCORPION_ICACHE_ACCESS,
  436. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = SCORPION_ICACHE_MISS,
  437. /*
  438. * Only ITLB misses and DTLB refills are supported. If users want the
  439. * DTLB refills misses a raw counter must be used.
  440. */
  441. [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = SCORPION_DTLB_ACCESS,
  442. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = SCORPION_DTLB_MISS,
  443. [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = SCORPION_DTLB_ACCESS,
  444. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = SCORPION_DTLB_MISS,
  445. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = SCORPION_ITLB_MISS,
  446. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = SCORPION_ITLB_MISS,
  447. [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  448. [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  449. [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_PRED,
  450. [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  451. };
  452. PMU_FORMAT_ATTR(event, "config:0-7");
  453. static struct attribute *armv7_pmu_format_attrs[] = {
  454. &format_attr_event.attr,
  455. NULL,
  456. };
  457. static struct attribute_group armv7_pmu_format_attr_group = {
  458. .name = "format",
  459. .attrs = armv7_pmu_format_attrs,
  460. };
  461. #define ARMV7_EVENT_ATTR_RESOLVE(m) #m
  462. #define ARMV7_EVENT_ATTR(name, config) \
  463. PMU_EVENT_ATTR_STRING(name, armv7_event_attr_##name, \
  464. "event=" ARMV7_EVENT_ATTR_RESOLVE(config))
  465. ARMV7_EVENT_ATTR(sw_incr, ARMV7_PERFCTR_PMNC_SW_INCR);
  466. ARMV7_EVENT_ATTR(l1i_cache_refill, ARMV7_PERFCTR_L1_ICACHE_REFILL);
  467. ARMV7_EVENT_ATTR(l1i_tlb_refill, ARMV7_PERFCTR_ITLB_REFILL);
  468. ARMV7_EVENT_ATTR(l1d_cache_refill, ARMV7_PERFCTR_L1_DCACHE_REFILL);
  469. ARMV7_EVENT_ATTR(l1d_cache, ARMV7_PERFCTR_L1_DCACHE_ACCESS);
  470. ARMV7_EVENT_ATTR(l1d_tlb_refill, ARMV7_PERFCTR_DTLB_REFILL);
  471. ARMV7_EVENT_ATTR(ld_retired, ARMV7_PERFCTR_MEM_READ);
  472. ARMV7_EVENT_ATTR(st_retired, ARMV7_PERFCTR_MEM_WRITE);
  473. ARMV7_EVENT_ATTR(inst_retired, ARMV7_PERFCTR_INSTR_EXECUTED);
  474. ARMV7_EVENT_ATTR(exc_taken, ARMV7_PERFCTR_EXC_TAKEN);
  475. ARMV7_EVENT_ATTR(exc_return, ARMV7_PERFCTR_EXC_EXECUTED);
  476. ARMV7_EVENT_ATTR(cid_write_retired, ARMV7_PERFCTR_CID_WRITE);
  477. ARMV7_EVENT_ATTR(pc_write_retired, ARMV7_PERFCTR_PC_WRITE);
  478. ARMV7_EVENT_ATTR(br_immed_retired, ARMV7_PERFCTR_PC_IMM_BRANCH);
  479. ARMV7_EVENT_ATTR(br_return_retired, ARMV7_PERFCTR_PC_PROC_RETURN);
  480. ARMV7_EVENT_ATTR(unaligned_ldst_retired, ARMV7_PERFCTR_MEM_UNALIGNED_ACCESS);
  481. ARMV7_EVENT_ATTR(br_mis_pred, ARMV7_PERFCTR_PC_BRANCH_MIS_PRED);
  482. ARMV7_EVENT_ATTR(cpu_cycles, ARMV7_PERFCTR_CLOCK_CYCLES);
  483. ARMV7_EVENT_ATTR(br_pred, ARMV7_PERFCTR_PC_BRANCH_PRED);
  484. static struct attribute *armv7_pmuv1_event_attrs[] = {
  485. &armv7_event_attr_sw_incr.attr.attr,
  486. &armv7_event_attr_l1i_cache_refill.attr.attr,
  487. &armv7_event_attr_l1i_tlb_refill.attr.attr,
  488. &armv7_event_attr_l1d_cache_refill.attr.attr,
  489. &armv7_event_attr_l1d_cache.attr.attr,
  490. &armv7_event_attr_l1d_tlb_refill.attr.attr,
  491. &armv7_event_attr_ld_retired.attr.attr,
  492. &armv7_event_attr_st_retired.attr.attr,
  493. &armv7_event_attr_inst_retired.attr.attr,
  494. &armv7_event_attr_exc_taken.attr.attr,
  495. &armv7_event_attr_exc_return.attr.attr,
  496. &armv7_event_attr_cid_write_retired.attr.attr,
  497. &armv7_event_attr_pc_write_retired.attr.attr,
  498. &armv7_event_attr_br_immed_retired.attr.attr,
  499. &armv7_event_attr_br_return_retired.attr.attr,
  500. &armv7_event_attr_unaligned_ldst_retired.attr.attr,
  501. &armv7_event_attr_br_mis_pred.attr.attr,
  502. &armv7_event_attr_cpu_cycles.attr.attr,
  503. &armv7_event_attr_br_pred.attr.attr,
  504. NULL,
  505. };
  506. static struct attribute_group armv7_pmuv1_events_attr_group = {
  507. .name = "events",
  508. .attrs = armv7_pmuv1_event_attrs,
  509. };
  510. static const struct attribute_group *armv7_pmuv1_attr_groups[] = {
  511. &armv7_pmuv1_events_attr_group,
  512. &armv7_pmu_format_attr_group,
  513. NULL,
  514. };
  515. ARMV7_EVENT_ATTR(mem_access, ARMV7_PERFCTR_MEM_ACCESS);
  516. ARMV7_EVENT_ATTR(l1i_cache, ARMV7_PERFCTR_L1_ICACHE_ACCESS);
  517. ARMV7_EVENT_ATTR(l1d_cache_wb, ARMV7_PERFCTR_L1_DCACHE_WB);
  518. ARMV7_EVENT_ATTR(l2d_cache, ARMV7_PERFCTR_L2_CACHE_ACCESS);
  519. ARMV7_EVENT_ATTR(l2d_cache_refill, ARMV7_PERFCTR_L2_CACHE_REFILL);
  520. ARMV7_EVENT_ATTR(l2d_cache_wb, ARMV7_PERFCTR_L2_CACHE_WB);
  521. ARMV7_EVENT_ATTR(bus_access, ARMV7_PERFCTR_BUS_ACCESS);
  522. ARMV7_EVENT_ATTR(memory_error, ARMV7_PERFCTR_MEM_ERROR);
  523. ARMV7_EVENT_ATTR(inst_spec, ARMV7_PERFCTR_INSTR_SPEC);
  524. ARMV7_EVENT_ATTR(ttbr_write_retired, ARMV7_PERFCTR_TTBR_WRITE);
  525. ARMV7_EVENT_ATTR(bus_cycles, ARMV7_PERFCTR_BUS_CYCLES);
  526. static struct attribute *armv7_pmuv2_event_attrs[] = {
  527. &armv7_event_attr_sw_incr.attr.attr,
  528. &armv7_event_attr_l1i_cache_refill.attr.attr,
  529. &armv7_event_attr_l1i_tlb_refill.attr.attr,
  530. &armv7_event_attr_l1d_cache_refill.attr.attr,
  531. &armv7_event_attr_l1d_cache.attr.attr,
  532. &armv7_event_attr_l1d_tlb_refill.attr.attr,
  533. &armv7_event_attr_ld_retired.attr.attr,
  534. &armv7_event_attr_st_retired.attr.attr,
  535. &armv7_event_attr_inst_retired.attr.attr,
  536. &armv7_event_attr_exc_taken.attr.attr,
  537. &armv7_event_attr_exc_return.attr.attr,
  538. &armv7_event_attr_cid_write_retired.attr.attr,
  539. &armv7_event_attr_pc_write_retired.attr.attr,
  540. &armv7_event_attr_br_immed_retired.attr.attr,
  541. &armv7_event_attr_br_return_retired.attr.attr,
  542. &armv7_event_attr_unaligned_ldst_retired.attr.attr,
  543. &armv7_event_attr_br_mis_pred.attr.attr,
  544. &armv7_event_attr_cpu_cycles.attr.attr,
  545. &armv7_event_attr_br_pred.attr.attr,
  546. &armv7_event_attr_mem_access.attr.attr,
  547. &armv7_event_attr_l1i_cache.attr.attr,
  548. &armv7_event_attr_l1d_cache_wb.attr.attr,
  549. &armv7_event_attr_l2d_cache.attr.attr,
  550. &armv7_event_attr_l2d_cache_refill.attr.attr,
  551. &armv7_event_attr_l2d_cache_wb.attr.attr,
  552. &armv7_event_attr_bus_access.attr.attr,
  553. &armv7_event_attr_memory_error.attr.attr,
  554. &armv7_event_attr_inst_spec.attr.attr,
  555. &armv7_event_attr_ttbr_write_retired.attr.attr,
  556. &armv7_event_attr_bus_cycles.attr.attr,
  557. NULL,
  558. };
  559. static struct attribute_group armv7_pmuv2_events_attr_group = {
  560. .name = "events",
  561. .attrs = armv7_pmuv2_event_attrs,
  562. };
  563. static const struct attribute_group *armv7_pmuv2_attr_groups[] = {
  564. &armv7_pmuv2_events_attr_group,
  565. &armv7_pmu_format_attr_group,
  566. NULL,
  567. };
  568. /*
  569. * Perf Events' indices
  570. */
  571. #define ARMV7_IDX_CYCLE_COUNTER 0
  572. #define ARMV7_IDX_COUNTER0 1
  573. #define ARMV7_IDX_COUNTER_LAST(cpu_pmu) \
  574. (ARMV7_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
  575. #define ARMV7_MAX_COUNTERS 32
  576. #define ARMV7_COUNTER_MASK (ARMV7_MAX_COUNTERS - 1)
  577. /*
  578. * ARMv7 low level PMNC access
  579. */
  580. /*
  581. * Perf Event to low level counters mapping
  582. */
  583. #define ARMV7_IDX_TO_COUNTER(x) \
  584. (((x) - ARMV7_IDX_COUNTER0) & ARMV7_COUNTER_MASK)
  585. /*
  586. * Per-CPU PMNC: config reg
  587. */
  588. #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
  589. #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
  590. #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
  591. #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  592. #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
  593. #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  594. #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
  595. #define ARMV7_PMNC_N_MASK 0x1f
  596. #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
  597. /*
  598. * FLAG: counters overflow flag status reg
  599. */
  600. #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
  601. #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
  602. /*
  603. * PMXEVTYPER: Event selection reg
  604. */
  605. #define ARMV7_EVTYPE_MASK 0xc80000ff /* Mask for writable bits */
  606. #define ARMV7_EVTYPE_EVENT 0xff /* Mask for EVENT bits */
  607. /*
  608. * Event filters for PMUv2
  609. */
  610. #define ARMV7_EXCLUDE_PL1 (1 << 31)
  611. #define ARMV7_EXCLUDE_USER (1 << 30)
  612. #define ARMV7_INCLUDE_HYP (1 << 27)
  613. static inline u32 armv7_pmnc_read(void)
  614. {
  615. u32 val;
  616. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
  617. return val;
  618. }
  619. static inline void armv7_pmnc_write(u32 val)
  620. {
  621. val &= ARMV7_PMNC_MASK;
  622. isb();
  623. asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
  624. }
  625. static inline int armv7_pmnc_has_overflowed(u32 pmnc)
  626. {
  627. return pmnc & ARMV7_OVERFLOWED_MASK;
  628. }
  629. static inline int armv7_pmnc_counter_valid(struct arm_pmu *cpu_pmu, int idx)
  630. {
  631. return idx >= ARMV7_IDX_CYCLE_COUNTER &&
  632. idx <= ARMV7_IDX_COUNTER_LAST(cpu_pmu);
  633. }
  634. static inline int armv7_pmnc_counter_has_overflowed(u32 pmnc, int idx)
  635. {
  636. return pmnc & BIT(ARMV7_IDX_TO_COUNTER(idx));
  637. }
  638. static inline void armv7_pmnc_select_counter(int idx)
  639. {
  640. u32 counter = ARMV7_IDX_TO_COUNTER(idx);
  641. asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (counter));
  642. isb();
  643. }
  644. static inline u32 armv7pmu_read_counter(struct perf_event *event)
  645. {
  646. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  647. struct hw_perf_event *hwc = &event->hw;
  648. int idx = hwc->idx;
  649. u32 value = 0;
  650. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  651. pr_err("CPU%u reading wrong counter %d\n",
  652. smp_processor_id(), idx);
  653. } else if (idx == ARMV7_IDX_CYCLE_COUNTER) {
  654. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
  655. } else {
  656. armv7_pmnc_select_counter(idx);
  657. asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (value));
  658. }
  659. return value;
  660. }
  661. static inline void armv7pmu_write_counter(struct perf_event *event, u32 value)
  662. {
  663. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  664. struct hw_perf_event *hwc = &event->hw;
  665. int idx = hwc->idx;
  666. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  667. pr_err("CPU%u writing wrong counter %d\n",
  668. smp_processor_id(), idx);
  669. } else if (idx == ARMV7_IDX_CYCLE_COUNTER) {
  670. asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
  671. } else {
  672. armv7_pmnc_select_counter(idx);
  673. asm volatile("mcr p15, 0, %0, c9, c13, 2" : : "r" (value));
  674. }
  675. }
  676. static inline void armv7_pmnc_write_evtsel(int idx, u32 val)
  677. {
  678. armv7_pmnc_select_counter(idx);
  679. val &= ARMV7_EVTYPE_MASK;
  680. asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
  681. }
  682. static inline void armv7_pmnc_enable_counter(int idx)
  683. {
  684. u32 counter = ARMV7_IDX_TO_COUNTER(idx);
  685. asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (BIT(counter)));
  686. }
  687. static inline void armv7_pmnc_disable_counter(int idx)
  688. {
  689. u32 counter = ARMV7_IDX_TO_COUNTER(idx);
  690. asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (BIT(counter)));
  691. }
  692. static inline void armv7_pmnc_enable_intens(int idx)
  693. {
  694. u32 counter = ARMV7_IDX_TO_COUNTER(idx);
  695. asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (BIT(counter)));
  696. }
  697. static inline void armv7_pmnc_disable_intens(int idx)
  698. {
  699. u32 counter = ARMV7_IDX_TO_COUNTER(idx);
  700. asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (BIT(counter)));
  701. isb();
  702. /* Clear the overflow flag in case an interrupt is pending. */
  703. asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (BIT(counter)));
  704. isb();
  705. }
  706. static inline u32 armv7_pmnc_getreset_flags(void)
  707. {
  708. u32 val;
  709. /* Read */
  710. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  711. /* Write to clear flags */
  712. val &= ARMV7_FLAG_MASK;
  713. asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
  714. return val;
  715. }
  716. #ifdef DEBUG
  717. static void armv7_pmnc_dump_regs(struct arm_pmu *cpu_pmu)
  718. {
  719. u32 val;
  720. unsigned int cnt;
  721. pr_info("PMNC registers dump:\n");
  722. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
  723. pr_info("PMNC =0x%08x\n", val);
  724. asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
  725. pr_info("CNTENS=0x%08x\n", val);
  726. asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
  727. pr_info("INTENS=0x%08x\n", val);
  728. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  729. pr_info("FLAGS =0x%08x\n", val);
  730. asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
  731. pr_info("SELECT=0x%08x\n", val);
  732. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
  733. pr_info("CCNT =0x%08x\n", val);
  734. for (cnt = ARMV7_IDX_COUNTER0;
  735. cnt <= ARMV7_IDX_COUNTER_LAST(cpu_pmu); cnt++) {
  736. armv7_pmnc_select_counter(cnt);
  737. asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
  738. pr_info("CNT[%d] count =0x%08x\n",
  739. ARMV7_IDX_TO_COUNTER(cnt), val);
  740. asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
  741. pr_info("CNT[%d] evtsel=0x%08x\n",
  742. ARMV7_IDX_TO_COUNTER(cnt), val);
  743. }
  744. }
  745. #endif
  746. static void armv7pmu_enable_event(struct perf_event *event)
  747. {
  748. unsigned long flags;
  749. struct hw_perf_event *hwc = &event->hw;
  750. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  751. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  752. int idx = hwc->idx;
  753. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  754. pr_err("CPU%u enabling wrong PMNC counter IRQ enable %d\n",
  755. smp_processor_id(), idx);
  756. return;
  757. }
  758. /*
  759. * Enable counter and interrupt, and set the counter to count
  760. * the event that we're interested in.
  761. */
  762. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  763. /*
  764. * Disable counter
  765. */
  766. armv7_pmnc_disable_counter(idx);
  767. /*
  768. * Set event (if destined for PMNx counters)
  769. * We only need to set the event for the cycle counter if we
  770. * have the ability to perform event filtering.
  771. */
  772. if (cpu_pmu->set_event_filter || idx != ARMV7_IDX_CYCLE_COUNTER)
  773. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  774. /*
  775. * Enable interrupt for this counter
  776. */
  777. armv7_pmnc_enable_intens(idx);
  778. /*
  779. * Enable counter
  780. */
  781. armv7_pmnc_enable_counter(idx);
  782. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  783. }
  784. static void armv7pmu_disable_event(struct perf_event *event)
  785. {
  786. unsigned long flags;
  787. struct hw_perf_event *hwc = &event->hw;
  788. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  789. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  790. int idx = hwc->idx;
  791. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  792. pr_err("CPU%u disabling wrong PMNC counter IRQ enable %d\n",
  793. smp_processor_id(), idx);
  794. return;
  795. }
  796. /*
  797. * Disable counter and interrupt
  798. */
  799. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  800. /*
  801. * Disable counter
  802. */
  803. armv7_pmnc_disable_counter(idx);
  804. /*
  805. * Disable interrupt for this counter
  806. */
  807. armv7_pmnc_disable_intens(idx);
  808. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  809. }
  810. static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev)
  811. {
  812. u32 pmnc;
  813. struct perf_sample_data data;
  814. struct arm_pmu *cpu_pmu = (struct arm_pmu *)dev;
  815. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  816. struct pt_regs *regs;
  817. int idx;
  818. /*
  819. * Get and reset the IRQ flags
  820. */
  821. pmnc = armv7_pmnc_getreset_flags();
  822. /*
  823. * Did an overflow occur?
  824. */
  825. if (!armv7_pmnc_has_overflowed(pmnc))
  826. return IRQ_NONE;
  827. /*
  828. * Handle the counter(s) overflow(s)
  829. */
  830. regs = get_irq_regs();
  831. for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
  832. struct perf_event *event = cpuc->events[idx];
  833. struct hw_perf_event *hwc;
  834. /* Ignore if we don't have an event. */
  835. if (!event)
  836. continue;
  837. /*
  838. * We have a single interrupt for all counters. Check that
  839. * each counter has overflowed before we process it.
  840. */
  841. if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
  842. continue;
  843. hwc = &event->hw;
  844. armpmu_event_update(event);
  845. perf_sample_data_init(&data, 0, hwc->last_period);
  846. if (!armpmu_event_set_period(event))
  847. continue;
  848. if (perf_event_overflow(event, &data, regs))
  849. cpu_pmu->disable(event);
  850. }
  851. /*
  852. * Handle the pending perf events.
  853. *
  854. * Note: this call *must* be run with interrupts disabled. For
  855. * platforms that can have the PMU interrupts raised as an NMI, this
  856. * will not work.
  857. */
  858. irq_work_run();
  859. return IRQ_HANDLED;
  860. }
  861. static void armv7pmu_start(struct arm_pmu *cpu_pmu)
  862. {
  863. unsigned long flags;
  864. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  865. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  866. /* Enable all counters */
  867. armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
  868. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  869. }
  870. static void armv7pmu_stop(struct arm_pmu *cpu_pmu)
  871. {
  872. unsigned long flags;
  873. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  874. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  875. /* Disable all counters */
  876. armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
  877. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  878. }
  879. static int armv7pmu_get_event_idx(struct pmu_hw_events *cpuc,
  880. struct perf_event *event)
  881. {
  882. int idx;
  883. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  884. struct hw_perf_event *hwc = &event->hw;
  885. unsigned long evtype = hwc->config_base & ARMV7_EVTYPE_EVENT;
  886. /* Always place a cycle counter into the cycle counter. */
  887. if (evtype == ARMV7_PERFCTR_CPU_CYCLES) {
  888. if (test_and_set_bit(ARMV7_IDX_CYCLE_COUNTER, cpuc->used_mask))
  889. return -EAGAIN;
  890. return ARMV7_IDX_CYCLE_COUNTER;
  891. }
  892. /*
  893. * For anything other than a cycle counter, try and use
  894. * the events counters
  895. */
  896. for (idx = ARMV7_IDX_COUNTER0; idx < cpu_pmu->num_events; ++idx) {
  897. if (!test_and_set_bit(idx, cpuc->used_mask))
  898. return idx;
  899. }
  900. /* The counters are all in use. */
  901. return -EAGAIN;
  902. }
  903. /*
  904. * Add an event filter to a given event. This will only work for PMUv2 PMUs.
  905. */
  906. static int armv7pmu_set_event_filter(struct hw_perf_event *event,
  907. struct perf_event_attr *attr)
  908. {
  909. unsigned long config_base = 0;
  910. if (attr->exclude_idle)
  911. return -EPERM;
  912. if (attr->exclude_user)
  913. config_base |= ARMV7_EXCLUDE_USER;
  914. if (attr->exclude_kernel)
  915. config_base |= ARMV7_EXCLUDE_PL1;
  916. if (!attr->exclude_hv)
  917. config_base |= ARMV7_INCLUDE_HYP;
  918. /*
  919. * Install the filter into config_base as this is used to
  920. * construct the event type.
  921. */
  922. event->config_base = config_base;
  923. return 0;
  924. }
  925. static void armv7pmu_reset(void *info)
  926. {
  927. struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
  928. u32 idx, nb_cnt = cpu_pmu->num_events;
  929. /* The counter and interrupt enable registers are unknown at reset. */
  930. for (idx = ARMV7_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) {
  931. armv7_pmnc_disable_counter(idx);
  932. armv7_pmnc_disable_intens(idx);
  933. }
  934. /* Initialize & Reset PMNC: C and P bits */
  935. armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
  936. }
  937. static int armv7_a8_map_event(struct perf_event *event)
  938. {
  939. return armpmu_map_event(event, &armv7_a8_perf_map,
  940. &armv7_a8_perf_cache_map, 0xFF);
  941. }
  942. static int armv7_a9_map_event(struct perf_event *event)
  943. {
  944. return armpmu_map_event(event, &armv7_a9_perf_map,
  945. &armv7_a9_perf_cache_map, 0xFF);
  946. }
  947. static int armv7_a5_map_event(struct perf_event *event)
  948. {
  949. return armpmu_map_event(event, &armv7_a5_perf_map,
  950. &armv7_a5_perf_cache_map, 0xFF);
  951. }
  952. static int armv7_a15_map_event(struct perf_event *event)
  953. {
  954. return armpmu_map_event(event, &armv7_a15_perf_map,
  955. &armv7_a15_perf_cache_map, 0xFF);
  956. }
  957. static int armv7_a7_map_event(struct perf_event *event)
  958. {
  959. return armpmu_map_event(event, &armv7_a7_perf_map,
  960. &armv7_a7_perf_cache_map, 0xFF);
  961. }
  962. static int armv7_a12_map_event(struct perf_event *event)
  963. {
  964. return armpmu_map_event(event, &armv7_a12_perf_map,
  965. &armv7_a12_perf_cache_map, 0xFF);
  966. }
  967. static int krait_map_event(struct perf_event *event)
  968. {
  969. return armpmu_map_event(event, &krait_perf_map,
  970. &krait_perf_cache_map, 0xFFFFF);
  971. }
  972. static int krait_map_event_no_branch(struct perf_event *event)
  973. {
  974. return armpmu_map_event(event, &krait_perf_map_no_branch,
  975. &krait_perf_cache_map, 0xFFFFF);
  976. }
  977. static int scorpion_map_event(struct perf_event *event)
  978. {
  979. return armpmu_map_event(event, &scorpion_perf_map,
  980. &scorpion_perf_cache_map, 0xFFFFF);
  981. }
  982. static void armv7pmu_init(struct arm_pmu *cpu_pmu)
  983. {
  984. cpu_pmu->handle_irq = armv7pmu_handle_irq;
  985. cpu_pmu->enable = armv7pmu_enable_event;
  986. cpu_pmu->disable = armv7pmu_disable_event;
  987. cpu_pmu->read_counter = armv7pmu_read_counter;
  988. cpu_pmu->write_counter = armv7pmu_write_counter;
  989. cpu_pmu->get_event_idx = armv7pmu_get_event_idx;
  990. cpu_pmu->start = armv7pmu_start;
  991. cpu_pmu->stop = armv7pmu_stop;
  992. cpu_pmu->reset = armv7pmu_reset;
  993. cpu_pmu->max_period = (1LLU << 32) - 1;
  994. };
  995. static void armv7_read_num_pmnc_events(void *info)
  996. {
  997. int *nb_cnt = info;
  998. /* Read the nb of CNTx counters supported from PMNC */
  999. *nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
  1000. /* Add the CPU cycles counter */
  1001. *nb_cnt += 1;
  1002. }
  1003. static int armv7_probe_num_events(struct arm_pmu *arm_pmu)
  1004. {
  1005. return smp_call_function_any(&arm_pmu->supported_cpus,
  1006. armv7_read_num_pmnc_events,
  1007. &arm_pmu->num_events, 1);
  1008. }
  1009. static int armv7_a8_pmu_init(struct arm_pmu *cpu_pmu)
  1010. {
  1011. armv7pmu_init(cpu_pmu);
  1012. cpu_pmu->name = "armv7_cortex_a8";
  1013. cpu_pmu->map_event = armv7_a8_map_event;
  1014. cpu_pmu->pmu.attr_groups = armv7_pmuv1_attr_groups;
  1015. return armv7_probe_num_events(cpu_pmu);
  1016. }
  1017. static int armv7_a9_pmu_init(struct arm_pmu *cpu_pmu)
  1018. {
  1019. armv7pmu_init(cpu_pmu);
  1020. cpu_pmu->name = "armv7_cortex_a9";
  1021. cpu_pmu->map_event = armv7_a9_map_event;
  1022. cpu_pmu->pmu.attr_groups = armv7_pmuv1_attr_groups;
  1023. return armv7_probe_num_events(cpu_pmu);
  1024. }
  1025. static int armv7_a5_pmu_init(struct arm_pmu *cpu_pmu)
  1026. {
  1027. armv7pmu_init(cpu_pmu);
  1028. cpu_pmu->name = "armv7_cortex_a5";
  1029. cpu_pmu->map_event = armv7_a5_map_event;
  1030. cpu_pmu->pmu.attr_groups = armv7_pmuv1_attr_groups;
  1031. return armv7_probe_num_events(cpu_pmu);
  1032. }
  1033. static int armv7_a15_pmu_init(struct arm_pmu *cpu_pmu)
  1034. {
  1035. armv7pmu_init(cpu_pmu);
  1036. cpu_pmu->name = "armv7_cortex_a15";
  1037. cpu_pmu->map_event = armv7_a15_map_event;
  1038. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1039. cpu_pmu->pmu.attr_groups = armv7_pmuv2_attr_groups;
  1040. return armv7_probe_num_events(cpu_pmu);
  1041. }
  1042. static int armv7_a7_pmu_init(struct arm_pmu *cpu_pmu)
  1043. {
  1044. armv7pmu_init(cpu_pmu);
  1045. cpu_pmu->name = "armv7_cortex_a7";
  1046. cpu_pmu->map_event = armv7_a7_map_event;
  1047. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1048. cpu_pmu->pmu.attr_groups = armv7_pmuv2_attr_groups;
  1049. return armv7_probe_num_events(cpu_pmu);
  1050. }
  1051. static int armv7_a12_pmu_init(struct arm_pmu *cpu_pmu)
  1052. {
  1053. armv7pmu_init(cpu_pmu);
  1054. cpu_pmu->name = "armv7_cortex_a12";
  1055. cpu_pmu->map_event = armv7_a12_map_event;
  1056. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1057. cpu_pmu->pmu.attr_groups = armv7_pmuv2_attr_groups;
  1058. return armv7_probe_num_events(cpu_pmu);
  1059. }
  1060. static int armv7_a17_pmu_init(struct arm_pmu *cpu_pmu)
  1061. {
  1062. int ret = armv7_a12_pmu_init(cpu_pmu);
  1063. cpu_pmu->name = "armv7_cortex_a17";
  1064. cpu_pmu->pmu.attr_groups = armv7_pmuv2_attr_groups;
  1065. return ret;
  1066. }
  1067. /*
  1068. * Krait Performance Monitor Region Event Selection Register (PMRESRn)
  1069. *
  1070. * 31 30 24 16 8 0
  1071. * +--------------------------------+
  1072. * PMRESR0 | EN | CC | CC | CC | CC | N = 1, R = 0
  1073. * +--------------------------------+
  1074. * PMRESR1 | EN | CC | CC | CC | CC | N = 1, R = 1
  1075. * +--------------------------------+
  1076. * PMRESR2 | EN | CC | CC | CC | CC | N = 1, R = 2
  1077. * +--------------------------------+
  1078. * VPMRESR0 | EN | CC | CC | CC | CC | N = 2, R = ?
  1079. * +--------------------------------+
  1080. * EN | G=3 | G=2 | G=1 | G=0
  1081. *
  1082. * Event Encoding:
  1083. *
  1084. * hwc->config_base = 0xNRCCG
  1085. *
  1086. * N = prefix, 1 for Krait CPU (PMRESRn), 2 for Venum VFP (VPMRESR)
  1087. * R = region register
  1088. * CC = class of events the group G is choosing from
  1089. * G = group or particular event
  1090. *
  1091. * Example: 0x12021 is a Krait CPU event in PMRESR2's group 1 with code 2
  1092. *
  1093. * A region (R) corresponds to a piece of the CPU (execution unit, instruction
  1094. * unit, etc.) while the event code (CC) corresponds to a particular class of
  1095. * events (interrupts for example). An event code is broken down into
  1096. * groups (G) that can be mapped into the PMU (irq, fiqs, and irq+fiqs for
  1097. * example).
  1098. */
  1099. #define KRAIT_EVENT (1 << 16)
  1100. #define VENUM_EVENT (2 << 16)
  1101. #define KRAIT_EVENT_MASK (KRAIT_EVENT | VENUM_EVENT)
  1102. #define PMRESRn_EN BIT(31)
  1103. #define EVENT_REGION(event) (((event) >> 12) & 0xf) /* R */
  1104. #define EVENT_GROUP(event) ((event) & 0xf) /* G */
  1105. #define EVENT_CODE(event) (((event) >> 4) & 0xff) /* CC */
  1106. #define EVENT_VENUM(event) (!!(event & VENUM_EVENT)) /* N=2 */
  1107. #define EVENT_CPU(event) (!!(event & KRAIT_EVENT)) /* N=1 */
  1108. static u32 krait_read_pmresrn(int n)
  1109. {
  1110. u32 val;
  1111. switch (n) {
  1112. case 0:
  1113. asm volatile("mrc p15, 1, %0, c9, c15, 0" : "=r" (val));
  1114. break;
  1115. case 1:
  1116. asm volatile("mrc p15, 1, %0, c9, c15, 1" : "=r" (val));
  1117. break;
  1118. case 2:
  1119. asm volatile("mrc p15, 1, %0, c9, c15, 2" : "=r" (val));
  1120. break;
  1121. default:
  1122. BUG(); /* Should be validated in krait_pmu_get_event_idx() */
  1123. }
  1124. return val;
  1125. }
  1126. static void krait_write_pmresrn(int n, u32 val)
  1127. {
  1128. switch (n) {
  1129. case 0:
  1130. asm volatile("mcr p15, 1, %0, c9, c15, 0" : : "r" (val));
  1131. break;
  1132. case 1:
  1133. asm volatile("mcr p15, 1, %0, c9, c15, 1" : : "r" (val));
  1134. break;
  1135. case 2:
  1136. asm volatile("mcr p15, 1, %0, c9, c15, 2" : : "r" (val));
  1137. break;
  1138. default:
  1139. BUG(); /* Should be validated in krait_pmu_get_event_idx() */
  1140. }
  1141. }
  1142. static u32 venum_read_pmresr(void)
  1143. {
  1144. u32 val;
  1145. asm volatile("mrc p10, 7, %0, c11, c0, 0" : "=r" (val));
  1146. return val;
  1147. }
  1148. static void venum_write_pmresr(u32 val)
  1149. {
  1150. asm volatile("mcr p10, 7, %0, c11, c0, 0" : : "r" (val));
  1151. }
  1152. static void venum_pre_pmresr(u32 *venum_orig_val, u32 *fp_orig_val)
  1153. {
  1154. u32 venum_new_val;
  1155. u32 fp_new_val;
  1156. BUG_ON(preemptible());
  1157. /* CPACR Enable CP10 and CP11 access */
  1158. *venum_orig_val = get_copro_access();
  1159. venum_new_val = *venum_orig_val | CPACC_SVC(10) | CPACC_SVC(11);
  1160. set_copro_access(venum_new_val);
  1161. /* Enable FPEXC */
  1162. *fp_orig_val = fmrx(FPEXC);
  1163. fp_new_val = *fp_orig_val | FPEXC_EN;
  1164. fmxr(FPEXC, fp_new_val);
  1165. }
  1166. static void venum_post_pmresr(u32 venum_orig_val, u32 fp_orig_val)
  1167. {
  1168. BUG_ON(preemptible());
  1169. /* Restore FPEXC */
  1170. fmxr(FPEXC, fp_orig_val);
  1171. isb();
  1172. /* Restore CPACR */
  1173. set_copro_access(venum_orig_val);
  1174. }
  1175. static u32 krait_get_pmresrn_event(unsigned int region)
  1176. {
  1177. static const u32 pmresrn_table[] = { KRAIT_PMRESR0_GROUP0,
  1178. KRAIT_PMRESR1_GROUP0,
  1179. KRAIT_PMRESR2_GROUP0 };
  1180. return pmresrn_table[region];
  1181. }
  1182. static void krait_evt_setup(int idx, u32 config_base)
  1183. {
  1184. u32 val;
  1185. u32 mask;
  1186. u32 vval, fval;
  1187. unsigned int region = EVENT_REGION(config_base);
  1188. unsigned int group = EVENT_GROUP(config_base);
  1189. unsigned int code = EVENT_CODE(config_base);
  1190. unsigned int group_shift;
  1191. bool venum_event = EVENT_VENUM(config_base);
  1192. group_shift = group * 8;
  1193. mask = 0xff << group_shift;
  1194. /* Configure evtsel for the region and group */
  1195. if (venum_event)
  1196. val = KRAIT_VPMRESR0_GROUP0;
  1197. else
  1198. val = krait_get_pmresrn_event(region);
  1199. val += group;
  1200. /* Mix in mode-exclusion bits */
  1201. val |= config_base & (ARMV7_EXCLUDE_USER | ARMV7_EXCLUDE_PL1);
  1202. armv7_pmnc_write_evtsel(idx, val);
  1203. if (venum_event) {
  1204. venum_pre_pmresr(&vval, &fval);
  1205. val = venum_read_pmresr();
  1206. val &= ~mask;
  1207. val |= code << group_shift;
  1208. val |= PMRESRn_EN;
  1209. venum_write_pmresr(val);
  1210. venum_post_pmresr(vval, fval);
  1211. } else {
  1212. val = krait_read_pmresrn(region);
  1213. val &= ~mask;
  1214. val |= code << group_shift;
  1215. val |= PMRESRn_EN;
  1216. krait_write_pmresrn(region, val);
  1217. }
  1218. }
  1219. static u32 clear_pmresrn_group(u32 val, int group)
  1220. {
  1221. u32 mask;
  1222. int group_shift;
  1223. group_shift = group * 8;
  1224. mask = 0xff << group_shift;
  1225. val &= ~mask;
  1226. /* Don't clear enable bit if entire region isn't disabled */
  1227. if (val & ~PMRESRn_EN)
  1228. return val |= PMRESRn_EN;
  1229. return 0;
  1230. }
  1231. static void krait_clearpmu(u32 config_base)
  1232. {
  1233. u32 val;
  1234. u32 vval, fval;
  1235. unsigned int region = EVENT_REGION(config_base);
  1236. unsigned int group = EVENT_GROUP(config_base);
  1237. bool venum_event = EVENT_VENUM(config_base);
  1238. if (venum_event) {
  1239. venum_pre_pmresr(&vval, &fval);
  1240. val = venum_read_pmresr();
  1241. val = clear_pmresrn_group(val, group);
  1242. venum_write_pmresr(val);
  1243. venum_post_pmresr(vval, fval);
  1244. } else {
  1245. val = krait_read_pmresrn(region);
  1246. val = clear_pmresrn_group(val, group);
  1247. krait_write_pmresrn(region, val);
  1248. }
  1249. }
  1250. static void krait_pmu_disable_event(struct perf_event *event)
  1251. {
  1252. unsigned long flags;
  1253. struct hw_perf_event *hwc = &event->hw;
  1254. int idx = hwc->idx;
  1255. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1256. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  1257. /* Disable counter and interrupt */
  1258. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  1259. /* Disable counter */
  1260. armv7_pmnc_disable_counter(idx);
  1261. /*
  1262. * Clear pmresr code (if destined for PMNx counters)
  1263. */
  1264. if (hwc->config_base & KRAIT_EVENT_MASK)
  1265. krait_clearpmu(hwc->config_base);
  1266. /* Disable interrupt for this counter */
  1267. armv7_pmnc_disable_intens(idx);
  1268. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  1269. }
  1270. static void krait_pmu_enable_event(struct perf_event *event)
  1271. {
  1272. unsigned long flags;
  1273. struct hw_perf_event *hwc = &event->hw;
  1274. int idx = hwc->idx;
  1275. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1276. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  1277. /*
  1278. * Enable counter and interrupt, and set the counter to count
  1279. * the event that we're interested in.
  1280. */
  1281. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  1282. /* Disable counter */
  1283. armv7_pmnc_disable_counter(idx);
  1284. /*
  1285. * Set event (if destined for PMNx counters)
  1286. * We set the event for the cycle counter because we
  1287. * have the ability to perform event filtering.
  1288. */
  1289. if (hwc->config_base & KRAIT_EVENT_MASK)
  1290. krait_evt_setup(idx, hwc->config_base);
  1291. else
  1292. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1293. /* Enable interrupt for this counter */
  1294. armv7_pmnc_enable_intens(idx);
  1295. /* Enable counter */
  1296. armv7_pmnc_enable_counter(idx);
  1297. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  1298. }
  1299. static void krait_pmu_reset(void *info)
  1300. {
  1301. u32 vval, fval;
  1302. struct arm_pmu *cpu_pmu = info;
  1303. u32 idx, nb_cnt = cpu_pmu->num_events;
  1304. armv7pmu_reset(info);
  1305. /* Clear all pmresrs */
  1306. krait_write_pmresrn(0, 0);
  1307. krait_write_pmresrn(1, 0);
  1308. krait_write_pmresrn(2, 0);
  1309. venum_pre_pmresr(&vval, &fval);
  1310. venum_write_pmresr(0);
  1311. venum_post_pmresr(vval, fval);
  1312. /* Reset PMxEVNCTCR to sane default */
  1313. for (idx = ARMV7_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) {
  1314. armv7_pmnc_select_counter(idx);
  1315. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1316. }
  1317. }
  1318. static int krait_event_to_bit(struct perf_event *event, unsigned int region,
  1319. unsigned int group)
  1320. {
  1321. int bit;
  1322. struct hw_perf_event *hwc = &event->hw;
  1323. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1324. if (hwc->config_base & VENUM_EVENT)
  1325. bit = KRAIT_VPMRESR0_GROUP0;
  1326. else
  1327. bit = krait_get_pmresrn_event(region);
  1328. bit -= krait_get_pmresrn_event(0);
  1329. bit += group;
  1330. /*
  1331. * Lower bits are reserved for use by the counters (see
  1332. * armv7pmu_get_event_idx() for more info)
  1333. */
  1334. bit += ARMV7_IDX_COUNTER_LAST(cpu_pmu) + 1;
  1335. return bit;
  1336. }
  1337. /*
  1338. * We check for column exclusion constraints here.
  1339. * Two events cant use the same group within a pmresr register.
  1340. */
  1341. static int krait_pmu_get_event_idx(struct pmu_hw_events *cpuc,
  1342. struct perf_event *event)
  1343. {
  1344. int idx;
  1345. int bit = -1;
  1346. struct hw_perf_event *hwc = &event->hw;
  1347. unsigned int region = EVENT_REGION(hwc->config_base);
  1348. unsigned int code = EVENT_CODE(hwc->config_base);
  1349. unsigned int group = EVENT_GROUP(hwc->config_base);
  1350. bool venum_event = EVENT_VENUM(hwc->config_base);
  1351. bool krait_event = EVENT_CPU(hwc->config_base);
  1352. if (venum_event || krait_event) {
  1353. /* Ignore invalid events */
  1354. if (group > 3 || region > 2)
  1355. return -EINVAL;
  1356. if (venum_event && (code & 0xe0))
  1357. return -EINVAL;
  1358. bit = krait_event_to_bit(event, region, group);
  1359. if (test_and_set_bit(bit, cpuc->used_mask))
  1360. return -EAGAIN;
  1361. }
  1362. idx = armv7pmu_get_event_idx(cpuc, event);
  1363. if (idx < 0 && bit >= 0)
  1364. clear_bit(bit, cpuc->used_mask);
  1365. return idx;
  1366. }
  1367. static void krait_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  1368. struct perf_event *event)
  1369. {
  1370. int bit;
  1371. struct hw_perf_event *hwc = &event->hw;
  1372. unsigned int region = EVENT_REGION(hwc->config_base);
  1373. unsigned int group = EVENT_GROUP(hwc->config_base);
  1374. bool venum_event = EVENT_VENUM(hwc->config_base);
  1375. bool krait_event = EVENT_CPU(hwc->config_base);
  1376. if (venum_event || krait_event) {
  1377. bit = krait_event_to_bit(event, region, group);
  1378. clear_bit(bit, cpuc->used_mask);
  1379. }
  1380. }
  1381. static int krait_pmu_init(struct arm_pmu *cpu_pmu)
  1382. {
  1383. armv7pmu_init(cpu_pmu);
  1384. cpu_pmu->name = "armv7_krait";
  1385. /* Some early versions of Krait don't support PC write events */
  1386. if (of_property_read_bool(cpu_pmu->plat_device->dev.of_node,
  1387. "qcom,no-pc-write"))
  1388. cpu_pmu->map_event = krait_map_event_no_branch;
  1389. else
  1390. cpu_pmu->map_event = krait_map_event;
  1391. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1392. cpu_pmu->reset = krait_pmu_reset;
  1393. cpu_pmu->enable = krait_pmu_enable_event;
  1394. cpu_pmu->disable = krait_pmu_disable_event;
  1395. cpu_pmu->get_event_idx = krait_pmu_get_event_idx;
  1396. cpu_pmu->clear_event_idx = krait_pmu_clear_event_idx;
  1397. return armv7_probe_num_events(cpu_pmu);
  1398. }
  1399. /*
  1400. * Scorpion Local Performance Monitor Register (LPMn)
  1401. *
  1402. * 31 30 24 16 8 0
  1403. * +--------------------------------+
  1404. * LPM0 | EN | CC | CC | CC | CC | N = 1, R = 0
  1405. * +--------------------------------+
  1406. * LPM1 | EN | CC | CC | CC | CC | N = 1, R = 1
  1407. * +--------------------------------+
  1408. * LPM2 | EN | CC | CC | CC | CC | N = 1, R = 2
  1409. * +--------------------------------+
  1410. * L2LPM | EN | CC | CC | CC | CC | N = 1, R = 3
  1411. * +--------------------------------+
  1412. * VLPM | EN | CC | CC | CC | CC | N = 2, R = ?
  1413. * +--------------------------------+
  1414. * EN | G=3 | G=2 | G=1 | G=0
  1415. *
  1416. *
  1417. * Event Encoding:
  1418. *
  1419. * hwc->config_base = 0xNRCCG
  1420. *
  1421. * N = prefix, 1 for Scorpion CPU (LPMn/L2LPM), 2 for Venum VFP (VLPM)
  1422. * R = region register
  1423. * CC = class of events the group G is choosing from
  1424. * G = group or particular event
  1425. *
  1426. * Example: 0x12021 is a Scorpion CPU event in LPM2's group 1 with code 2
  1427. *
  1428. * A region (R) corresponds to a piece of the CPU (execution unit, instruction
  1429. * unit, etc.) while the event code (CC) corresponds to a particular class of
  1430. * events (interrupts for example). An event code is broken down into
  1431. * groups (G) that can be mapped into the PMU (irq, fiqs, and irq+fiqs for
  1432. * example).
  1433. */
  1434. static u32 scorpion_read_pmresrn(int n)
  1435. {
  1436. u32 val;
  1437. switch (n) {
  1438. case 0:
  1439. asm volatile("mrc p15, 0, %0, c15, c0, 0" : "=r" (val));
  1440. break;
  1441. case 1:
  1442. asm volatile("mrc p15, 1, %0, c15, c0, 0" : "=r" (val));
  1443. break;
  1444. case 2:
  1445. asm volatile("mrc p15, 2, %0, c15, c0, 0" : "=r" (val));
  1446. break;
  1447. case 3:
  1448. asm volatile("mrc p15, 3, %0, c15, c2, 0" : "=r" (val));
  1449. break;
  1450. default:
  1451. BUG(); /* Should be validated in scorpion_pmu_get_event_idx() */
  1452. }
  1453. return val;
  1454. }
  1455. static void scorpion_write_pmresrn(int n, u32 val)
  1456. {
  1457. switch (n) {
  1458. case 0:
  1459. asm volatile("mcr p15, 0, %0, c15, c0, 0" : : "r" (val));
  1460. break;
  1461. case 1:
  1462. asm volatile("mcr p15, 1, %0, c15, c0, 0" : : "r" (val));
  1463. break;
  1464. case 2:
  1465. asm volatile("mcr p15, 2, %0, c15, c0, 0" : : "r" (val));
  1466. break;
  1467. case 3:
  1468. asm volatile("mcr p15, 3, %0, c15, c2, 0" : : "r" (val));
  1469. break;
  1470. default:
  1471. BUG(); /* Should be validated in scorpion_pmu_get_event_idx() */
  1472. }
  1473. }
  1474. static u32 scorpion_get_pmresrn_event(unsigned int region)
  1475. {
  1476. static const u32 pmresrn_table[] = { SCORPION_LPM0_GROUP0,
  1477. SCORPION_LPM1_GROUP0,
  1478. SCORPION_LPM2_GROUP0,
  1479. SCORPION_L2LPM_GROUP0 };
  1480. return pmresrn_table[region];
  1481. }
  1482. static void scorpion_evt_setup(int idx, u32 config_base)
  1483. {
  1484. u32 val;
  1485. u32 mask;
  1486. u32 vval, fval;
  1487. unsigned int region = EVENT_REGION(config_base);
  1488. unsigned int group = EVENT_GROUP(config_base);
  1489. unsigned int code = EVENT_CODE(config_base);
  1490. unsigned int group_shift;
  1491. bool venum_event = EVENT_VENUM(config_base);
  1492. group_shift = group * 8;
  1493. mask = 0xff << group_shift;
  1494. /* Configure evtsel for the region and group */
  1495. if (venum_event)
  1496. val = SCORPION_VLPM_GROUP0;
  1497. else
  1498. val = scorpion_get_pmresrn_event(region);
  1499. val += group;
  1500. /* Mix in mode-exclusion bits */
  1501. val |= config_base & (ARMV7_EXCLUDE_USER | ARMV7_EXCLUDE_PL1);
  1502. armv7_pmnc_write_evtsel(idx, val);
  1503. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1504. if (venum_event) {
  1505. venum_pre_pmresr(&vval, &fval);
  1506. val = venum_read_pmresr();
  1507. val &= ~mask;
  1508. val |= code << group_shift;
  1509. val |= PMRESRn_EN;
  1510. venum_write_pmresr(val);
  1511. venum_post_pmresr(vval, fval);
  1512. } else {
  1513. val = scorpion_read_pmresrn(region);
  1514. val &= ~mask;
  1515. val |= code << group_shift;
  1516. val |= PMRESRn_EN;
  1517. scorpion_write_pmresrn(region, val);
  1518. }
  1519. }
  1520. static void scorpion_clearpmu(u32 config_base)
  1521. {
  1522. u32 val;
  1523. u32 vval, fval;
  1524. unsigned int region = EVENT_REGION(config_base);
  1525. unsigned int group = EVENT_GROUP(config_base);
  1526. bool venum_event = EVENT_VENUM(config_base);
  1527. if (venum_event) {
  1528. venum_pre_pmresr(&vval, &fval);
  1529. val = venum_read_pmresr();
  1530. val = clear_pmresrn_group(val, group);
  1531. venum_write_pmresr(val);
  1532. venum_post_pmresr(vval, fval);
  1533. } else {
  1534. val = scorpion_read_pmresrn(region);
  1535. val = clear_pmresrn_group(val, group);
  1536. scorpion_write_pmresrn(region, val);
  1537. }
  1538. }
  1539. static void scorpion_pmu_disable_event(struct perf_event *event)
  1540. {
  1541. unsigned long flags;
  1542. struct hw_perf_event *hwc = &event->hw;
  1543. int idx = hwc->idx;
  1544. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1545. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  1546. /* Disable counter and interrupt */
  1547. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  1548. /* Disable counter */
  1549. armv7_pmnc_disable_counter(idx);
  1550. /*
  1551. * Clear pmresr code (if destined for PMNx counters)
  1552. */
  1553. if (hwc->config_base & KRAIT_EVENT_MASK)
  1554. scorpion_clearpmu(hwc->config_base);
  1555. /* Disable interrupt for this counter */
  1556. armv7_pmnc_disable_intens(idx);
  1557. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  1558. }
  1559. static void scorpion_pmu_enable_event(struct perf_event *event)
  1560. {
  1561. unsigned long flags;
  1562. struct hw_perf_event *hwc = &event->hw;
  1563. int idx = hwc->idx;
  1564. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1565. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  1566. /*
  1567. * Enable counter and interrupt, and set the counter to count
  1568. * the event that we're interested in.
  1569. */
  1570. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  1571. /* Disable counter */
  1572. armv7_pmnc_disable_counter(idx);
  1573. /*
  1574. * Set event (if destined for PMNx counters)
  1575. * We don't set the event for the cycle counter because we
  1576. * don't have the ability to perform event filtering.
  1577. */
  1578. if (hwc->config_base & KRAIT_EVENT_MASK)
  1579. scorpion_evt_setup(idx, hwc->config_base);
  1580. else if (idx != ARMV7_IDX_CYCLE_COUNTER)
  1581. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1582. /* Enable interrupt for this counter */
  1583. armv7_pmnc_enable_intens(idx);
  1584. /* Enable counter */
  1585. armv7_pmnc_enable_counter(idx);
  1586. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  1587. }
  1588. static void scorpion_pmu_reset(void *info)
  1589. {
  1590. u32 vval, fval;
  1591. struct arm_pmu *cpu_pmu = info;
  1592. u32 idx, nb_cnt = cpu_pmu->num_events;
  1593. armv7pmu_reset(info);
  1594. /* Clear all pmresrs */
  1595. scorpion_write_pmresrn(0, 0);
  1596. scorpion_write_pmresrn(1, 0);
  1597. scorpion_write_pmresrn(2, 0);
  1598. scorpion_write_pmresrn(3, 0);
  1599. venum_pre_pmresr(&vval, &fval);
  1600. venum_write_pmresr(0);
  1601. venum_post_pmresr(vval, fval);
  1602. /* Reset PMxEVNCTCR to sane default */
  1603. for (idx = ARMV7_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) {
  1604. armv7_pmnc_select_counter(idx);
  1605. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1606. }
  1607. }
  1608. static int scorpion_event_to_bit(struct perf_event *event, unsigned int region,
  1609. unsigned int group)
  1610. {
  1611. int bit;
  1612. struct hw_perf_event *hwc = &event->hw;
  1613. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1614. if (hwc->config_base & VENUM_EVENT)
  1615. bit = SCORPION_VLPM_GROUP0;
  1616. else
  1617. bit = scorpion_get_pmresrn_event(region);
  1618. bit -= scorpion_get_pmresrn_event(0);
  1619. bit += group;
  1620. /*
  1621. * Lower bits are reserved for use by the counters (see
  1622. * armv7pmu_get_event_idx() for more info)
  1623. */
  1624. bit += ARMV7_IDX_COUNTER_LAST(cpu_pmu) + 1;
  1625. return bit;
  1626. }
  1627. /*
  1628. * We check for column exclusion constraints here.
  1629. * Two events cant use the same group within a pmresr register.
  1630. */
  1631. static int scorpion_pmu_get_event_idx(struct pmu_hw_events *cpuc,
  1632. struct perf_event *event)
  1633. {
  1634. int idx;
  1635. int bit = -1;
  1636. struct hw_perf_event *hwc = &event->hw;
  1637. unsigned int region = EVENT_REGION(hwc->config_base);
  1638. unsigned int group = EVENT_GROUP(hwc->config_base);
  1639. bool venum_event = EVENT_VENUM(hwc->config_base);
  1640. bool scorpion_event = EVENT_CPU(hwc->config_base);
  1641. if (venum_event || scorpion_event) {
  1642. /* Ignore invalid events */
  1643. if (group > 3 || region > 3)
  1644. return -EINVAL;
  1645. bit = scorpion_event_to_bit(event, region, group);
  1646. if (test_and_set_bit(bit, cpuc->used_mask))
  1647. return -EAGAIN;
  1648. }
  1649. idx = armv7pmu_get_event_idx(cpuc, event);
  1650. if (idx < 0 && bit >= 0)
  1651. clear_bit(bit, cpuc->used_mask);
  1652. return idx;
  1653. }
  1654. static void scorpion_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  1655. struct perf_event *event)
  1656. {
  1657. int bit;
  1658. struct hw_perf_event *hwc = &event->hw;
  1659. unsigned int region = EVENT_REGION(hwc->config_base);
  1660. unsigned int group = EVENT_GROUP(hwc->config_base);
  1661. bool venum_event = EVENT_VENUM(hwc->config_base);
  1662. bool scorpion_event = EVENT_CPU(hwc->config_base);
  1663. if (venum_event || scorpion_event) {
  1664. bit = scorpion_event_to_bit(event, region, group);
  1665. clear_bit(bit, cpuc->used_mask);
  1666. }
  1667. }
  1668. static int scorpion_pmu_init(struct arm_pmu *cpu_pmu)
  1669. {
  1670. armv7pmu_init(cpu_pmu);
  1671. cpu_pmu->name = "armv7_scorpion";
  1672. cpu_pmu->map_event = scorpion_map_event;
  1673. cpu_pmu->reset = scorpion_pmu_reset;
  1674. cpu_pmu->enable = scorpion_pmu_enable_event;
  1675. cpu_pmu->disable = scorpion_pmu_disable_event;
  1676. cpu_pmu->get_event_idx = scorpion_pmu_get_event_idx;
  1677. cpu_pmu->clear_event_idx = scorpion_pmu_clear_event_idx;
  1678. return armv7_probe_num_events(cpu_pmu);
  1679. }
  1680. static int scorpion_mp_pmu_init(struct arm_pmu *cpu_pmu)
  1681. {
  1682. armv7pmu_init(cpu_pmu);
  1683. cpu_pmu->name = "armv7_scorpion_mp";
  1684. cpu_pmu->map_event = scorpion_map_event;
  1685. cpu_pmu->reset = scorpion_pmu_reset;
  1686. cpu_pmu->enable = scorpion_pmu_enable_event;
  1687. cpu_pmu->disable = scorpion_pmu_disable_event;
  1688. cpu_pmu->get_event_idx = scorpion_pmu_get_event_idx;
  1689. cpu_pmu->clear_event_idx = scorpion_pmu_clear_event_idx;
  1690. return armv7_probe_num_events(cpu_pmu);
  1691. }
  1692. static const struct of_device_id armv7_pmu_of_device_ids[] = {
  1693. {.compatible = "arm,cortex-a17-pmu", .data = armv7_a17_pmu_init},
  1694. {.compatible = "arm,cortex-a15-pmu", .data = armv7_a15_pmu_init},
  1695. {.compatible = "arm,cortex-a12-pmu", .data = armv7_a12_pmu_init},
  1696. {.compatible = "arm,cortex-a9-pmu", .data = armv7_a9_pmu_init},
  1697. {.compatible = "arm,cortex-a8-pmu", .data = armv7_a8_pmu_init},
  1698. {.compatible = "arm,cortex-a7-pmu", .data = armv7_a7_pmu_init},
  1699. {.compatible = "arm,cortex-a5-pmu", .data = armv7_a5_pmu_init},
  1700. {.compatible = "qcom,krait-pmu", .data = krait_pmu_init},
  1701. {.compatible = "qcom,scorpion-pmu", .data = scorpion_pmu_init},
  1702. {.compatible = "qcom,scorpion-mp-pmu", .data = scorpion_mp_pmu_init},
  1703. {},
  1704. };
  1705. static const struct pmu_probe_info armv7_pmu_probe_table[] = {
  1706. ARM_PMU_PROBE(ARM_CPU_PART_CORTEX_A8, armv7_a8_pmu_init),
  1707. ARM_PMU_PROBE(ARM_CPU_PART_CORTEX_A9, armv7_a9_pmu_init),
  1708. { /* sentinel value */ }
  1709. };
  1710. static int armv7_pmu_device_probe(struct platform_device *pdev)
  1711. {
  1712. return arm_pmu_device_probe(pdev, armv7_pmu_of_device_ids,
  1713. armv7_pmu_probe_table);
  1714. }
  1715. static struct platform_driver armv7_pmu_driver = {
  1716. .driver = {
  1717. .name = "armv7-pmu",
  1718. .of_match_table = armv7_pmu_of_device_ids,
  1719. },
  1720. .probe = armv7_pmu_device_probe,
  1721. };
  1722. static int __init register_armv7_pmu_driver(void)
  1723. {
  1724. return platform_driver_register(&armv7_pmu_driver);
  1725. }
  1726. device_initcall(register_armv7_pmu_driver);
  1727. #endif /* CONFIG_CPU_V7 */