arm_v7_pmu.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
  4. *
  5. * ARMv7 support: Jean Pihet <jpihet@mvista.com>
  6. * 2010 (c) MontaVista Software, LLC.
  7. *
  8. * Copied from ARMv6 code, with the low level code inspired
  9. * by the ARMv7 Oprofile code.
  10. *
  11. * Cortex-A8 has up to 4 configurable performance counters and
  12. * a single cycle counter.
  13. * Cortex-A9 has up to 31 configurable performance counters and
  14. * a single cycle counter.
  15. *
  16. * All counters can be enabled/disabled and IRQ masked separately. The cycle
  17. * counter and all 4 performance counters together can be reset separately.
  18. */
  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. ARMV7_EVENT_ATTR(mem_access, ARMV7_PERFCTR_MEM_ACCESS);
  511. ARMV7_EVENT_ATTR(l1i_cache, ARMV7_PERFCTR_L1_ICACHE_ACCESS);
  512. ARMV7_EVENT_ATTR(l1d_cache_wb, ARMV7_PERFCTR_L1_DCACHE_WB);
  513. ARMV7_EVENT_ATTR(l2d_cache, ARMV7_PERFCTR_L2_CACHE_ACCESS);
  514. ARMV7_EVENT_ATTR(l2d_cache_refill, ARMV7_PERFCTR_L2_CACHE_REFILL);
  515. ARMV7_EVENT_ATTR(l2d_cache_wb, ARMV7_PERFCTR_L2_CACHE_WB);
  516. ARMV7_EVENT_ATTR(bus_access, ARMV7_PERFCTR_BUS_ACCESS);
  517. ARMV7_EVENT_ATTR(memory_error, ARMV7_PERFCTR_MEM_ERROR);
  518. ARMV7_EVENT_ATTR(inst_spec, ARMV7_PERFCTR_INSTR_SPEC);
  519. ARMV7_EVENT_ATTR(ttbr_write_retired, ARMV7_PERFCTR_TTBR_WRITE);
  520. ARMV7_EVENT_ATTR(bus_cycles, ARMV7_PERFCTR_BUS_CYCLES);
  521. static struct attribute *armv7_pmuv2_event_attrs[] = {
  522. &armv7_event_attr_sw_incr.attr.attr,
  523. &armv7_event_attr_l1i_cache_refill.attr.attr,
  524. &armv7_event_attr_l1i_tlb_refill.attr.attr,
  525. &armv7_event_attr_l1d_cache_refill.attr.attr,
  526. &armv7_event_attr_l1d_cache.attr.attr,
  527. &armv7_event_attr_l1d_tlb_refill.attr.attr,
  528. &armv7_event_attr_ld_retired.attr.attr,
  529. &armv7_event_attr_st_retired.attr.attr,
  530. &armv7_event_attr_inst_retired.attr.attr,
  531. &armv7_event_attr_exc_taken.attr.attr,
  532. &armv7_event_attr_exc_return.attr.attr,
  533. &armv7_event_attr_cid_write_retired.attr.attr,
  534. &armv7_event_attr_pc_write_retired.attr.attr,
  535. &armv7_event_attr_br_immed_retired.attr.attr,
  536. &armv7_event_attr_br_return_retired.attr.attr,
  537. &armv7_event_attr_unaligned_ldst_retired.attr.attr,
  538. &armv7_event_attr_br_mis_pred.attr.attr,
  539. &armv7_event_attr_cpu_cycles.attr.attr,
  540. &armv7_event_attr_br_pred.attr.attr,
  541. &armv7_event_attr_mem_access.attr.attr,
  542. &armv7_event_attr_l1i_cache.attr.attr,
  543. &armv7_event_attr_l1d_cache_wb.attr.attr,
  544. &armv7_event_attr_l2d_cache.attr.attr,
  545. &armv7_event_attr_l2d_cache_refill.attr.attr,
  546. &armv7_event_attr_l2d_cache_wb.attr.attr,
  547. &armv7_event_attr_bus_access.attr.attr,
  548. &armv7_event_attr_memory_error.attr.attr,
  549. &armv7_event_attr_inst_spec.attr.attr,
  550. &armv7_event_attr_ttbr_write_retired.attr.attr,
  551. &armv7_event_attr_bus_cycles.attr.attr,
  552. NULL,
  553. };
  554. static struct attribute_group armv7_pmuv2_events_attr_group = {
  555. .name = "events",
  556. .attrs = armv7_pmuv2_event_attrs,
  557. };
  558. /*
  559. * Perf Events' indices
  560. */
  561. #define ARMV7_IDX_CYCLE_COUNTER 31
  562. #define ARMV7_IDX_COUNTER_MAX 31
  563. /*
  564. * ARMv7 low level PMNC access
  565. */
  566. /*
  567. * Per-CPU PMNC: config reg
  568. */
  569. #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
  570. #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
  571. #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
  572. #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  573. #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
  574. #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  575. #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
  576. #define ARMV7_PMNC_N_MASK 0x1f
  577. #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
  578. /*
  579. * FLAG: counters overflow flag status reg
  580. */
  581. #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
  582. #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
  583. /*
  584. * PMXEVTYPER: Event selection reg
  585. */
  586. #define ARMV7_EVTYPE_MASK 0xc80000ff /* Mask for writable bits */
  587. #define ARMV7_EVTYPE_EVENT 0xff /* Mask for EVENT bits */
  588. /*
  589. * Event filters for PMUv2
  590. */
  591. #define ARMV7_EXCLUDE_PL1 BIT(31)
  592. #define ARMV7_EXCLUDE_USER BIT(30)
  593. #define ARMV7_INCLUDE_HYP BIT(27)
  594. /*
  595. * Secure debug enable reg
  596. */
  597. #define ARMV7_SDER_SUNIDEN BIT(1) /* Permit non-invasive debug */
  598. static inline u32 armv7_pmnc_read(void)
  599. {
  600. u32 val;
  601. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
  602. return val;
  603. }
  604. static inline void armv7_pmnc_write(u32 val)
  605. {
  606. val &= ARMV7_PMNC_MASK;
  607. isb();
  608. asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
  609. }
  610. static inline int armv7_pmnc_has_overflowed(u32 pmnc)
  611. {
  612. return pmnc & ARMV7_OVERFLOWED_MASK;
  613. }
  614. static inline int armv7_pmnc_counter_valid(struct arm_pmu *cpu_pmu, int idx)
  615. {
  616. return test_bit(idx, cpu_pmu->cntr_mask);
  617. }
  618. static inline int armv7_pmnc_counter_has_overflowed(u32 pmnc, int idx)
  619. {
  620. return pmnc & BIT(idx);
  621. }
  622. static inline void armv7_pmnc_select_counter(int idx)
  623. {
  624. asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (idx));
  625. isb();
  626. }
  627. static inline u64 armv7pmu_read_counter(struct perf_event *event)
  628. {
  629. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  630. struct hw_perf_event *hwc = &event->hw;
  631. int idx = hwc->idx;
  632. u32 value = 0;
  633. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  634. pr_err("CPU%u reading wrong counter %d\n",
  635. smp_processor_id(), idx);
  636. } else if (idx == ARMV7_IDX_CYCLE_COUNTER) {
  637. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
  638. } else {
  639. armv7_pmnc_select_counter(idx);
  640. asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (value));
  641. }
  642. return value;
  643. }
  644. static inline void armv7pmu_write_counter(struct perf_event *event, u64 value)
  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. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  650. pr_err("CPU%u writing wrong counter %d\n",
  651. smp_processor_id(), idx);
  652. } else if (idx == ARMV7_IDX_CYCLE_COUNTER) {
  653. asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" ((u32)value));
  654. } else {
  655. armv7_pmnc_select_counter(idx);
  656. asm volatile("mcr p15, 0, %0, c9, c13, 2" : : "r" ((u32)value));
  657. }
  658. }
  659. static inline void armv7_pmnc_write_evtsel(int idx, u32 val)
  660. {
  661. armv7_pmnc_select_counter(idx);
  662. val &= ARMV7_EVTYPE_MASK;
  663. asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
  664. }
  665. static inline void armv7_pmnc_enable_counter(int idx)
  666. {
  667. asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (BIT(idx)));
  668. }
  669. static inline void armv7_pmnc_disable_counter(int idx)
  670. {
  671. asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (BIT(idx)));
  672. }
  673. static inline void armv7_pmnc_enable_intens(int idx)
  674. {
  675. asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (BIT(idx)));
  676. }
  677. static inline void armv7_pmnc_disable_intens(int idx)
  678. {
  679. asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (BIT(idx)));
  680. isb();
  681. /* Clear the overflow flag in case an interrupt is pending. */
  682. asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (BIT(idx)));
  683. isb();
  684. }
  685. static inline u32 armv7_pmnc_getreset_flags(void)
  686. {
  687. u32 val;
  688. /* Read */
  689. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  690. /* Write to clear flags */
  691. val &= ARMV7_FLAG_MASK;
  692. asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
  693. return val;
  694. }
  695. #ifdef DEBUG
  696. static void armv7_pmnc_dump_regs(struct arm_pmu *cpu_pmu)
  697. {
  698. u32 val;
  699. unsigned int cnt;
  700. pr_info("PMNC registers dump:\n");
  701. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
  702. pr_info("PMNC =0x%08x\n", val);
  703. asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
  704. pr_info("CNTENS=0x%08x\n", val);
  705. asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
  706. pr_info("INTENS=0x%08x\n", val);
  707. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  708. pr_info("FLAGS =0x%08x\n", val);
  709. asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
  710. pr_info("SELECT=0x%08x\n", val);
  711. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
  712. pr_info("CCNT =0x%08x\n", val);
  713. for_each_set_bit(cnt, cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX) {
  714. armv7_pmnc_select_counter(cnt);
  715. asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
  716. pr_info("CNT[%d] count =0x%08x\n", cnt, val);
  717. asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
  718. pr_info("CNT[%d] evtsel=0x%08x\n", cnt, val);
  719. }
  720. }
  721. #endif
  722. static void armv7pmu_enable_event(struct perf_event *event)
  723. {
  724. struct hw_perf_event *hwc = &event->hw;
  725. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  726. int idx = hwc->idx;
  727. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  728. pr_err("CPU%u enabling wrong PMNC counter IRQ enable %d\n",
  729. smp_processor_id(), idx);
  730. return;
  731. }
  732. /*
  733. * Set event (if destined for PMNx counters)
  734. * We only need to set the event for the cycle counter if we
  735. * have the ability to perform event filtering.
  736. */
  737. if (cpu_pmu->set_event_filter || idx != ARMV7_IDX_CYCLE_COUNTER)
  738. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  739. armv7_pmnc_enable_intens(idx);
  740. armv7_pmnc_enable_counter(idx);
  741. }
  742. static void armv7pmu_disable_event(struct perf_event *event)
  743. {
  744. struct hw_perf_event *hwc = &event->hw;
  745. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  746. int idx = hwc->idx;
  747. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  748. pr_err("CPU%u disabling wrong PMNC counter IRQ enable %d\n",
  749. smp_processor_id(), idx);
  750. return;
  751. }
  752. armv7_pmnc_disable_counter(idx);
  753. armv7_pmnc_disable_intens(idx);
  754. }
  755. static irqreturn_t armv7pmu_handle_irq(struct arm_pmu *cpu_pmu)
  756. {
  757. u32 pmnc;
  758. struct perf_sample_data data;
  759. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  760. struct pt_regs *regs;
  761. int idx;
  762. /*
  763. * Get and reset the IRQ flags
  764. */
  765. pmnc = armv7_pmnc_getreset_flags();
  766. /*
  767. * Did an overflow occur?
  768. */
  769. if (!armv7_pmnc_has_overflowed(pmnc))
  770. return IRQ_NONE;
  771. /*
  772. * Handle the counter(s) overflow(s)
  773. */
  774. regs = get_irq_regs();
  775. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
  776. struct perf_event *event = cpuc->events[idx];
  777. struct hw_perf_event *hwc;
  778. /* Ignore if we don't have an event. */
  779. if (!event)
  780. continue;
  781. /*
  782. * We have a single interrupt for all counters. Check that
  783. * each counter has overflowed before we process it.
  784. */
  785. if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
  786. continue;
  787. hwc = &event->hw;
  788. armpmu_event_update(event);
  789. perf_sample_data_init(&data, 0, hwc->last_period);
  790. if (!armpmu_event_set_period(event))
  791. continue;
  792. perf_event_overflow(event, &data, regs);
  793. }
  794. /*
  795. * Handle the pending perf events.
  796. *
  797. * Note: this call *must* be run with interrupts disabled. For
  798. * platforms that can have the PMU interrupts raised as an NMI, this
  799. * will not work.
  800. */
  801. irq_work_run();
  802. return IRQ_HANDLED;
  803. }
  804. static void armv7pmu_start(struct arm_pmu *cpu_pmu)
  805. {
  806. /* Enable all counters */
  807. armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
  808. }
  809. static void armv7pmu_stop(struct arm_pmu *cpu_pmu)
  810. {
  811. /* Disable all counters */
  812. armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
  813. }
  814. static int armv7pmu_get_event_idx(struct pmu_hw_events *cpuc,
  815. struct perf_event *event)
  816. {
  817. int idx;
  818. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  819. struct hw_perf_event *hwc = &event->hw;
  820. unsigned long evtype = hwc->config_base & ARMV7_EVTYPE_EVENT;
  821. /* Always place a cycle counter into the cycle counter. */
  822. if (evtype == ARMV7_PERFCTR_CPU_CYCLES) {
  823. if (test_and_set_bit(ARMV7_IDX_CYCLE_COUNTER, cpuc->used_mask))
  824. return -EAGAIN;
  825. return ARMV7_IDX_CYCLE_COUNTER;
  826. }
  827. /*
  828. * For anything other than a cycle counter, try and use
  829. * the events counters
  830. */
  831. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX) {
  832. if (!test_and_set_bit(idx, cpuc->used_mask))
  833. return idx;
  834. }
  835. /* The counters are all in use. */
  836. return -EAGAIN;
  837. }
  838. static void armv7pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  839. struct perf_event *event)
  840. {
  841. clear_bit(event->hw.idx, cpuc->used_mask);
  842. }
  843. /*
  844. * Add an event filter to a given event. This will only work for PMUv2 PMUs.
  845. */
  846. static int armv7pmu_set_event_filter(struct hw_perf_event *event,
  847. struct perf_event_attr *attr)
  848. {
  849. unsigned long config_base = 0;
  850. if (attr->exclude_idle) {
  851. pr_debug("ARM performance counters do not support mode exclusion\n");
  852. return -EOPNOTSUPP;
  853. }
  854. if (attr->exclude_user)
  855. config_base |= ARMV7_EXCLUDE_USER;
  856. if (attr->exclude_kernel)
  857. config_base |= ARMV7_EXCLUDE_PL1;
  858. if (!attr->exclude_hv)
  859. config_base |= ARMV7_INCLUDE_HYP;
  860. /*
  861. * Install the filter into config_base as this is used to
  862. * construct the event type.
  863. */
  864. event->config_base = config_base;
  865. return 0;
  866. }
  867. static void armv7pmu_reset(void *info)
  868. {
  869. struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
  870. u32 idx, val;
  871. if (cpu_pmu->secure_access) {
  872. asm volatile("mrc p15, 0, %0, c1, c1, 1" : "=r" (val));
  873. val |= ARMV7_SDER_SUNIDEN;
  874. asm volatile("mcr p15, 0, %0, c1, c1, 1" : : "r" (val));
  875. }
  876. /* The counter and interrupt enable registers are unknown at reset. */
  877. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
  878. armv7_pmnc_disable_counter(idx);
  879. armv7_pmnc_disable_intens(idx);
  880. }
  881. /* Initialize & Reset PMNC: C and P bits */
  882. armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
  883. }
  884. static int armv7_a8_map_event(struct perf_event *event)
  885. {
  886. return armpmu_map_event(event, &armv7_a8_perf_map,
  887. &armv7_a8_perf_cache_map, 0xFF);
  888. }
  889. static int armv7_a9_map_event(struct perf_event *event)
  890. {
  891. return armpmu_map_event(event, &armv7_a9_perf_map,
  892. &armv7_a9_perf_cache_map, 0xFF);
  893. }
  894. static int armv7_a5_map_event(struct perf_event *event)
  895. {
  896. return armpmu_map_event(event, &armv7_a5_perf_map,
  897. &armv7_a5_perf_cache_map, 0xFF);
  898. }
  899. static int armv7_a15_map_event(struct perf_event *event)
  900. {
  901. return armpmu_map_event(event, &armv7_a15_perf_map,
  902. &armv7_a15_perf_cache_map, 0xFF);
  903. }
  904. static int armv7_a7_map_event(struct perf_event *event)
  905. {
  906. return armpmu_map_event(event, &armv7_a7_perf_map,
  907. &armv7_a7_perf_cache_map, 0xFF);
  908. }
  909. static int armv7_a12_map_event(struct perf_event *event)
  910. {
  911. return armpmu_map_event(event, &armv7_a12_perf_map,
  912. &armv7_a12_perf_cache_map, 0xFF);
  913. }
  914. static int krait_map_event(struct perf_event *event)
  915. {
  916. return armpmu_map_event(event, &krait_perf_map,
  917. &krait_perf_cache_map, 0xFFFFF);
  918. }
  919. static int krait_map_event_no_branch(struct perf_event *event)
  920. {
  921. return armpmu_map_event(event, &krait_perf_map_no_branch,
  922. &krait_perf_cache_map, 0xFFFFF);
  923. }
  924. static int scorpion_map_event(struct perf_event *event)
  925. {
  926. return armpmu_map_event(event, &scorpion_perf_map,
  927. &scorpion_perf_cache_map, 0xFFFFF);
  928. }
  929. static void armv7pmu_init(struct arm_pmu *cpu_pmu)
  930. {
  931. cpu_pmu->handle_irq = armv7pmu_handle_irq;
  932. cpu_pmu->enable = armv7pmu_enable_event;
  933. cpu_pmu->disable = armv7pmu_disable_event;
  934. cpu_pmu->read_counter = armv7pmu_read_counter;
  935. cpu_pmu->write_counter = armv7pmu_write_counter;
  936. cpu_pmu->get_event_idx = armv7pmu_get_event_idx;
  937. cpu_pmu->clear_event_idx = armv7pmu_clear_event_idx;
  938. cpu_pmu->start = armv7pmu_start;
  939. cpu_pmu->stop = armv7pmu_stop;
  940. cpu_pmu->reset = armv7pmu_reset;
  941. };
  942. static void armv7_read_num_pmnc_events(void *info)
  943. {
  944. int nb_cnt;
  945. struct arm_pmu *cpu_pmu = info;
  946. /* Read the nb of CNTx counters supported from PMNC */
  947. nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
  948. bitmap_set(cpu_pmu->cntr_mask, 0, nb_cnt);
  949. /* Add the CPU cycles counter */
  950. set_bit(ARMV7_IDX_CYCLE_COUNTER, cpu_pmu->cntr_mask);
  951. }
  952. static int armv7_probe_num_events(struct arm_pmu *arm_pmu)
  953. {
  954. return smp_call_function_any(&arm_pmu->supported_cpus,
  955. armv7_read_num_pmnc_events,
  956. arm_pmu, 1);
  957. }
  958. static int armv7_a8_pmu_init(struct arm_pmu *cpu_pmu)
  959. {
  960. armv7pmu_init(cpu_pmu);
  961. cpu_pmu->name = "armv7_cortex_a8";
  962. cpu_pmu->map_event = armv7_a8_map_event;
  963. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  964. &armv7_pmuv1_events_attr_group;
  965. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  966. &armv7_pmu_format_attr_group;
  967. return armv7_probe_num_events(cpu_pmu);
  968. }
  969. static int armv7_a9_pmu_init(struct arm_pmu *cpu_pmu)
  970. {
  971. armv7pmu_init(cpu_pmu);
  972. cpu_pmu->name = "armv7_cortex_a9";
  973. cpu_pmu->map_event = armv7_a9_map_event;
  974. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  975. &armv7_pmuv1_events_attr_group;
  976. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  977. &armv7_pmu_format_attr_group;
  978. return armv7_probe_num_events(cpu_pmu);
  979. }
  980. static int armv7_a5_pmu_init(struct arm_pmu *cpu_pmu)
  981. {
  982. armv7pmu_init(cpu_pmu);
  983. cpu_pmu->name = "armv7_cortex_a5";
  984. cpu_pmu->map_event = armv7_a5_map_event;
  985. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  986. &armv7_pmuv1_events_attr_group;
  987. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  988. &armv7_pmu_format_attr_group;
  989. return armv7_probe_num_events(cpu_pmu);
  990. }
  991. static int armv7_a15_pmu_init(struct arm_pmu *cpu_pmu)
  992. {
  993. armv7pmu_init(cpu_pmu);
  994. cpu_pmu->name = "armv7_cortex_a15";
  995. cpu_pmu->map_event = armv7_a15_map_event;
  996. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  997. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  998. &armv7_pmuv2_events_attr_group;
  999. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  1000. &armv7_pmu_format_attr_group;
  1001. return armv7_probe_num_events(cpu_pmu);
  1002. }
  1003. static int armv7_a7_pmu_init(struct arm_pmu *cpu_pmu)
  1004. {
  1005. armv7pmu_init(cpu_pmu);
  1006. cpu_pmu->name = "armv7_cortex_a7";
  1007. cpu_pmu->map_event = armv7_a7_map_event;
  1008. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1009. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  1010. &armv7_pmuv2_events_attr_group;
  1011. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  1012. &armv7_pmu_format_attr_group;
  1013. return armv7_probe_num_events(cpu_pmu);
  1014. }
  1015. static int armv7_a12_pmu_init(struct arm_pmu *cpu_pmu)
  1016. {
  1017. armv7pmu_init(cpu_pmu);
  1018. cpu_pmu->name = "armv7_cortex_a12";
  1019. cpu_pmu->map_event = armv7_a12_map_event;
  1020. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1021. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  1022. &armv7_pmuv2_events_attr_group;
  1023. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  1024. &armv7_pmu_format_attr_group;
  1025. return armv7_probe_num_events(cpu_pmu);
  1026. }
  1027. static int armv7_a17_pmu_init(struct arm_pmu *cpu_pmu)
  1028. {
  1029. int ret = armv7_a12_pmu_init(cpu_pmu);
  1030. cpu_pmu->name = "armv7_cortex_a17";
  1031. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  1032. &armv7_pmuv2_events_attr_group;
  1033. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  1034. &armv7_pmu_format_attr_group;
  1035. return ret;
  1036. }
  1037. /*
  1038. * Krait Performance Monitor Region Event Selection Register (PMRESRn)
  1039. *
  1040. * 31 30 24 16 8 0
  1041. * +--------------------------------+
  1042. * PMRESR0 | EN | CC | CC | CC | CC | N = 1, R = 0
  1043. * +--------------------------------+
  1044. * PMRESR1 | EN | CC | CC | CC | CC | N = 1, R = 1
  1045. * +--------------------------------+
  1046. * PMRESR2 | EN | CC | CC | CC | CC | N = 1, R = 2
  1047. * +--------------------------------+
  1048. * VPMRESR0 | EN | CC | CC | CC | CC | N = 2, R = ?
  1049. * +--------------------------------+
  1050. * EN | G=3 | G=2 | G=1 | G=0
  1051. *
  1052. * Event Encoding:
  1053. *
  1054. * hwc->config_base = 0xNRCCG
  1055. *
  1056. * N = prefix, 1 for Krait CPU (PMRESRn), 2 for Venum VFP (VPMRESR)
  1057. * R = region register
  1058. * CC = class of events the group G is choosing from
  1059. * G = group or particular event
  1060. *
  1061. * Example: 0x12021 is a Krait CPU event in PMRESR2's group 1 with code 2
  1062. *
  1063. * A region (R) corresponds to a piece of the CPU (execution unit, instruction
  1064. * unit, etc.) while the event code (CC) corresponds to a particular class of
  1065. * events (interrupts for example). An event code is broken down into
  1066. * groups (G) that can be mapped into the PMU (irq, fiqs, and irq+fiqs for
  1067. * example).
  1068. */
  1069. #define KRAIT_EVENT (1 << 16)
  1070. #define VENUM_EVENT (2 << 16)
  1071. #define KRAIT_EVENT_MASK (KRAIT_EVENT | VENUM_EVENT)
  1072. #define PMRESRn_EN BIT(31)
  1073. #define EVENT_REGION(event) (((event) >> 12) & 0xf) /* R */
  1074. #define EVENT_GROUP(event) ((event) & 0xf) /* G */
  1075. #define EVENT_CODE(event) (((event) >> 4) & 0xff) /* CC */
  1076. #define EVENT_VENUM(event) (!!(event & VENUM_EVENT)) /* N=2 */
  1077. #define EVENT_CPU(event) (!!(event & KRAIT_EVENT)) /* N=1 */
  1078. static u32 krait_read_pmresrn(int n)
  1079. {
  1080. u32 val;
  1081. switch (n) {
  1082. case 0:
  1083. asm volatile("mrc p15, 1, %0, c9, c15, 0" : "=r" (val));
  1084. break;
  1085. case 1:
  1086. asm volatile("mrc p15, 1, %0, c9, c15, 1" : "=r" (val));
  1087. break;
  1088. case 2:
  1089. asm volatile("mrc p15, 1, %0, c9, c15, 2" : "=r" (val));
  1090. break;
  1091. default:
  1092. BUG(); /* Should be validated in krait_pmu_get_event_idx() */
  1093. }
  1094. return val;
  1095. }
  1096. static void krait_write_pmresrn(int n, u32 val)
  1097. {
  1098. switch (n) {
  1099. case 0:
  1100. asm volatile("mcr p15, 1, %0, c9, c15, 0" : : "r" (val));
  1101. break;
  1102. case 1:
  1103. asm volatile("mcr p15, 1, %0, c9, c15, 1" : : "r" (val));
  1104. break;
  1105. case 2:
  1106. asm volatile("mcr p15, 1, %0, c9, c15, 2" : : "r" (val));
  1107. break;
  1108. default:
  1109. BUG(); /* Should be validated in krait_pmu_get_event_idx() */
  1110. }
  1111. }
  1112. static u32 venum_read_pmresr(void)
  1113. {
  1114. u32 val;
  1115. asm volatile("mrc p10, 7, %0, c11, c0, 0" : "=r" (val));
  1116. return val;
  1117. }
  1118. static void venum_write_pmresr(u32 val)
  1119. {
  1120. asm volatile("mcr p10, 7, %0, c11, c0, 0" : : "r" (val));
  1121. }
  1122. static void venum_pre_pmresr(u32 *venum_orig_val, u32 *fp_orig_val)
  1123. {
  1124. u32 venum_new_val;
  1125. u32 fp_new_val;
  1126. BUG_ON(preemptible());
  1127. /* CPACR Enable CP10 and CP11 access */
  1128. *venum_orig_val = get_copro_access();
  1129. venum_new_val = *venum_orig_val | CPACC_SVC(10) | CPACC_SVC(11);
  1130. set_copro_access(venum_new_val);
  1131. /* Enable FPEXC */
  1132. *fp_orig_val = fmrx(FPEXC);
  1133. fp_new_val = *fp_orig_val | FPEXC_EN;
  1134. fmxr(FPEXC, fp_new_val);
  1135. }
  1136. static void venum_post_pmresr(u32 venum_orig_val, u32 fp_orig_val)
  1137. {
  1138. BUG_ON(preemptible());
  1139. /* Restore FPEXC */
  1140. fmxr(FPEXC, fp_orig_val);
  1141. isb();
  1142. /* Restore CPACR */
  1143. set_copro_access(venum_orig_val);
  1144. }
  1145. static u32 krait_get_pmresrn_event(unsigned int region)
  1146. {
  1147. static const u32 pmresrn_table[] = { KRAIT_PMRESR0_GROUP0,
  1148. KRAIT_PMRESR1_GROUP0,
  1149. KRAIT_PMRESR2_GROUP0 };
  1150. return pmresrn_table[region];
  1151. }
  1152. static void krait_evt_setup(int idx, u32 config_base)
  1153. {
  1154. u32 val;
  1155. u32 mask;
  1156. u32 vval, fval;
  1157. unsigned int region = EVENT_REGION(config_base);
  1158. unsigned int group = EVENT_GROUP(config_base);
  1159. unsigned int code = EVENT_CODE(config_base);
  1160. unsigned int group_shift;
  1161. bool venum_event = EVENT_VENUM(config_base);
  1162. group_shift = group * 8;
  1163. mask = 0xff << group_shift;
  1164. /* Configure evtsel for the region and group */
  1165. if (venum_event)
  1166. val = KRAIT_VPMRESR0_GROUP0;
  1167. else
  1168. val = krait_get_pmresrn_event(region);
  1169. val += group;
  1170. /* Mix in mode-exclusion bits */
  1171. val |= config_base & (ARMV7_EXCLUDE_USER | ARMV7_EXCLUDE_PL1);
  1172. armv7_pmnc_write_evtsel(idx, val);
  1173. if (venum_event) {
  1174. venum_pre_pmresr(&vval, &fval);
  1175. val = venum_read_pmresr();
  1176. val &= ~mask;
  1177. val |= code << group_shift;
  1178. val |= PMRESRn_EN;
  1179. venum_write_pmresr(val);
  1180. venum_post_pmresr(vval, fval);
  1181. } else {
  1182. val = krait_read_pmresrn(region);
  1183. val &= ~mask;
  1184. val |= code << group_shift;
  1185. val |= PMRESRn_EN;
  1186. krait_write_pmresrn(region, val);
  1187. }
  1188. }
  1189. static u32 clear_pmresrn_group(u32 val, int group)
  1190. {
  1191. u32 mask;
  1192. int group_shift;
  1193. group_shift = group * 8;
  1194. mask = 0xff << group_shift;
  1195. val &= ~mask;
  1196. /* Don't clear enable bit if entire region isn't disabled */
  1197. if (val & ~PMRESRn_EN)
  1198. return val |= PMRESRn_EN;
  1199. return 0;
  1200. }
  1201. static void krait_clearpmu(u32 config_base)
  1202. {
  1203. u32 val;
  1204. u32 vval, fval;
  1205. unsigned int region = EVENT_REGION(config_base);
  1206. unsigned int group = EVENT_GROUP(config_base);
  1207. bool venum_event = EVENT_VENUM(config_base);
  1208. if (venum_event) {
  1209. venum_pre_pmresr(&vval, &fval);
  1210. val = venum_read_pmresr();
  1211. val = clear_pmresrn_group(val, group);
  1212. venum_write_pmresr(val);
  1213. venum_post_pmresr(vval, fval);
  1214. } else {
  1215. val = krait_read_pmresrn(region);
  1216. val = clear_pmresrn_group(val, group);
  1217. krait_write_pmresrn(region, val);
  1218. }
  1219. }
  1220. static void krait_pmu_disable_event(struct perf_event *event)
  1221. {
  1222. struct hw_perf_event *hwc = &event->hw;
  1223. int idx = hwc->idx;
  1224. /* Disable counter and interrupt */
  1225. /* Disable counter */
  1226. armv7_pmnc_disable_counter(idx);
  1227. /*
  1228. * Clear pmresr code (if destined for PMNx counters)
  1229. */
  1230. if (hwc->config_base & KRAIT_EVENT_MASK)
  1231. krait_clearpmu(hwc->config_base);
  1232. /* Disable interrupt for this counter */
  1233. armv7_pmnc_disable_intens(idx);
  1234. }
  1235. static void krait_pmu_enable_event(struct perf_event *event)
  1236. {
  1237. struct hw_perf_event *hwc = &event->hw;
  1238. int idx = hwc->idx;
  1239. /*
  1240. * Set event (if destined for PMNx counters)
  1241. * We set the event for the cycle counter because we
  1242. * have the ability to perform event filtering.
  1243. */
  1244. if (hwc->config_base & KRAIT_EVENT_MASK)
  1245. krait_evt_setup(idx, hwc->config_base);
  1246. else
  1247. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1248. armv7_pmnc_enable_intens(idx);
  1249. armv7_pmnc_enable_counter(idx);
  1250. }
  1251. static void krait_pmu_reset(void *info)
  1252. {
  1253. u32 vval, fval;
  1254. struct arm_pmu *cpu_pmu = info;
  1255. u32 idx;
  1256. armv7pmu_reset(info);
  1257. /* Clear all pmresrs */
  1258. krait_write_pmresrn(0, 0);
  1259. krait_write_pmresrn(1, 0);
  1260. krait_write_pmresrn(2, 0);
  1261. venum_pre_pmresr(&vval, &fval);
  1262. venum_write_pmresr(0);
  1263. venum_post_pmresr(vval, fval);
  1264. /* Reset PMxEVNCTCR to sane default */
  1265. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX) {
  1266. armv7_pmnc_select_counter(idx);
  1267. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1268. }
  1269. }
  1270. static int krait_event_to_bit(struct perf_event *event, unsigned int region,
  1271. unsigned int group)
  1272. {
  1273. int bit;
  1274. struct hw_perf_event *hwc = &event->hw;
  1275. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1276. if (hwc->config_base & VENUM_EVENT)
  1277. bit = KRAIT_VPMRESR0_GROUP0;
  1278. else
  1279. bit = krait_get_pmresrn_event(region);
  1280. bit -= krait_get_pmresrn_event(0);
  1281. bit += group;
  1282. /*
  1283. * Lower bits are reserved for use by the counters (see
  1284. * armv7pmu_get_event_idx() for more info)
  1285. */
  1286. bit += bitmap_weight(cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX);
  1287. return bit;
  1288. }
  1289. /*
  1290. * We check for column exclusion constraints here.
  1291. * Two events cant use the same group within a pmresr register.
  1292. */
  1293. static int krait_pmu_get_event_idx(struct pmu_hw_events *cpuc,
  1294. struct perf_event *event)
  1295. {
  1296. int idx;
  1297. int bit = -1;
  1298. struct hw_perf_event *hwc = &event->hw;
  1299. unsigned int region = EVENT_REGION(hwc->config_base);
  1300. unsigned int code = EVENT_CODE(hwc->config_base);
  1301. unsigned int group = EVENT_GROUP(hwc->config_base);
  1302. bool venum_event = EVENT_VENUM(hwc->config_base);
  1303. bool krait_event = EVENT_CPU(hwc->config_base);
  1304. if (venum_event || krait_event) {
  1305. /* Ignore invalid events */
  1306. if (group > 3 || region > 2)
  1307. return -EINVAL;
  1308. if (venum_event && (code & 0xe0))
  1309. return -EINVAL;
  1310. bit = krait_event_to_bit(event, region, group);
  1311. if (test_and_set_bit(bit, cpuc->used_mask))
  1312. return -EAGAIN;
  1313. }
  1314. idx = armv7pmu_get_event_idx(cpuc, event);
  1315. if (idx < 0 && bit >= 0)
  1316. clear_bit(bit, cpuc->used_mask);
  1317. return idx;
  1318. }
  1319. static void krait_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  1320. struct perf_event *event)
  1321. {
  1322. int bit;
  1323. struct hw_perf_event *hwc = &event->hw;
  1324. unsigned int region = EVENT_REGION(hwc->config_base);
  1325. unsigned int group = EVENT_GROUP(hwc->config_base);
  1326. bool venum_event = EVENT_VENUM(hwc->config_base);
  1327. bool krait_event = EVENT_CPU(hwc->config_base);
  1328. armv7pmu_clear_event_idx(cpuc, event);
  1329. if (venum_event || krait_event) {
  1330. bit = krait_event_to_bit(event, region, group);
  1331. clear_bit(bit, cpuc->used_mask);
  1332. }
  1333. }
  1334. static int krait_pmu_init(struct arm_pmu *cpu_pmu)
  1335. {
  1336. armv7pmu_init(cpu_pmu);
  1337. cpu_pmu->name = "armv7_krait";
  1338. /* Some early versions of Krait don't support PC write events */
  1339. if (of_property_read_bool(cpu_pmu->plat_device->dev.of_node,
  1340. "qcom,no-pc-write"))
  1341. cpu_pmu->map_event = krait_map_event_no_branch;
  1342. else
  1343. cpu_pmu->map_event = krait_map_event;
  1344. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1345. cpu_pmu->reset = krait_pmu_reset;
  1346. cpu_pmu->enable = krait_pmu_enable_event;
  1347. cpu_pmu->disable = krait_pmu_disable_event;
  1348. cpu_pmu->get_event_idx = krait_pmu_get_event_idx;
  1349. cpu_pmu->clear_event_idx = krait_pmu_clear_event_idx;
  1350. return armv7_probe_num_events(cpu_pmu);
  1351. }
  1352. /*
  1353. * Scorpion Local Performance Monitor Register (LPMn)
  1354. *
  1355. * 31 30 24 16 8 0
  1356. * +--------------------------------+
  1357. * LPM0 | EN | CC | CC | CC | CC | N = 1, R = 0
  1358. * +--------------------------------+
  1359. * LPM1 | EN | CC | CC | CC | CC | N = 1, R = 1
  1360. * +--------------------------------+
  1361. * LPM2 | EN | CC | CC | CC | CC | N = 1, R = 2
  1362. * +--------------------------------+
  1363. * L2LPM | EN | CC | CC | CC | CC | N = 1, R = 3
  1364. * +--------------------------------+
  1365. * VLPM | EN | CC | CC | CC | CC | N = 2, R = ?
  1366. * +--------------------------------+
  1367. * EN | G=3 | G=2 | G=1 | G=0
  1368. *
  1369. *
  1370. * Event Encoding:
  1371. *
  1372. * hwc->config_base = 0xNRCCG
  1373. *
  1374. * N = prefix, 1 for Scorpion CPU (LPMn/L2LPM), 2 for Venum VFP (VLPM)
  1375. * R = region register
  1376. * CC = class of events the group G is choosing from
  1377. * G = group or particular event
  1378. *
  1379. * Example: 0x12021 is a Scorpion CPU event in LPM2's group 1 with code 2
  1380. *
  1381. * A region (R) corresponds to a piece of the CPU (execution unit, instruction
  1382. * unit, etc.) while the event code (CC) corresponds to a particular class of
  1383. * events (interrupts for example). An event code is broken down into
  1384. * groups (G) that can be mapped into the PMU (irq, fiqs, and irq+fiqs for
  1385. * example).
  1386. */
  1387. static u32 scorpion_read_pmresrn(int n)
  1388. {
  1389. u32 val;
  1390. switch (n) {
  1391. case 0:
  1392. asm volatile("mrc p15, 0, %0, c15, c0, 0" : "=r" (val));
  1393. break;
  1394. case 1:
  1395. asm volatile("mrc p15, 1, %0, c15, c0, 0" : "=r" (val));
  1396. break;
  1397. case 2:
  1398. asm volatile("mrc p15, 2, %0, c15, c0, 0" : "=r" (val));
  1399. break;
  1400. case 3:
  1401. asm volatile("mrc p15, 3, %0, c15, c2, 0" : "=r" (val));
  1402. break;
  1403. default:
  1404. BUG(); /* Should be validated in scorpion_pmu_get_event_idx() */
  1405. }
  1406. return val;
  1407. }
  1408. static void scorpion_write_pmresrn(int n, u32 val)
  1409. {
  1410. switch (n) {
  1411. case 0:
  1412. asm volatile("mcr p15, 0, %0, c15, c0, 0" : : "r" (val));
  1413. break;
  1414. case 1:
  1415. asm volatile("mcr p15, 1, %0, c15, c0, 0" : : "r" (val));
  1416. break;
  1417. case 2:
  1418. asm volatile("mcr p15, 2, %0, c15, c0, 0" : : "r" (val));
  1419. break;
  1420. case 3:
  1421. asm volatile("mcr p15, 3, %0, c15, c2, 0" : : "r" (val));
  1422. break;
  1423. default:
  1424. BUG(); /* Should be validated in scorpion_pmu_get_event_idx() */
  1425. }
  1426. }
  1427. static u32 scorpion_get_pmresrn_event(unsigned int region)
  1428. {
  1429. static const u32 pmresrn_table[] = { SCORPION_LPM0_GROUP0,
  1430. SCORPION_LPM1_GROUP0,
  1431. SCORPION_LPM2_GROUP0,
  1432. SCORPION_L2LPM_GROUP0 };
  1433. return pmresrn_table[region];
  1434. }
  1435. static void scorpion_evt_setup(int idx, u32 config_base)
  1436. {
  1437. u32 val;
  1438. u32 mask;
  1439. u32 vval, fval;
  1440. unsigned int region = EVENT_REGION(config_base);
  1441. unsigned int group = EVENT_GROUP(config_base);
  1442. unsigned int code = EVENT_CODE(config_base);
  1443. unsigned int group_shift;
  1444. bool venum_event = EVENT_VENUM(config_base);
  1445. group_shift = group * 8;
  1446. mask = 0xff << group_shift;
  1447. /* Configure evtsel for the region and group */
  1448. if (venum_event)
  1449. val = SCORPION_VLPM_GROUP0;
  1450. else
  1451. val = scorpion_get_pmresrn_event(region);
  1452. val += group;
  1453. /* Mix in mode-exclusion bits */
  1454. val |= config_base & (ARMV7_EXCLUDE_USER | ARMV7_EXCLUDE_PL1);
  1455. armv7_pmnc_write_evtsel(idx, val);
  1456. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1457. if (venum_event) {
  1458. venum_pre_pmresr(&vval, &fval);
  1459. val = venum_read_pmresr();
  1460. val &= ~mask;
  1461. val |= code << group_shift;
  1462. val |= PMRESRn_EN;
  1463. venum_write_pmresr(val);
  1464. venum_post_pmresr(vval, fval);
  1465. } else {
  1466. val = scorpion_read_pmresrn(region);
  1467. val &= ~mask;
  1468. val |= code << group_shift;
  1469. val |= PMRESRn_EN;
  1470. scorpion_write_pmresrn(region, val);
  1471. }
  1472. }
  1473. static void scorpion_clearpmu(u32 config_base)
  1474. {
  1475. u32 val;
  1476. u32 vval, fval;
  1477. unsigned int region = EVENT_REGION(config_base);
  1478. unsigned int group = EVENT_GROUP(config_base);
  1479. bool venum_event = EVENT_VENUM(config_base);
  1480. if (venum_event) {
  1481. venum_pre_pmresr(&vval, &fval);
  1482. val = venum_read_pmresr();
  1483. val = clear_pmresrn_group(val, group);
  1484. venum_write_pmresr(val);
  1485. venum_post_pmresr(vval, fval);
  1486. } else {
  1487. val = scorpion_read_pmresrn(region);
  1488. val = clear_pmresrn_group(val, group);
  1489. scorpion_write_pmresrn(region, val);
  1490. }
  1491. }
  1492. static void scorpion_pmu_disable_event(struct perf_event *event)
  1493. {
  1494. struct hw_perf_event *hwc = &event->hw;
  1495. int idx = hwc->idx;
  1496. /* Disable counter and interrupt */
  1497. /* Disable counter */
  1498. armv7_pmnc_disable_counter(idx);
  1499. /*
  1500. * Clear pmresr code (if destined for PMNx counters)
  1501. */
  1502. if (hwc->config_base & KRAIT_EVENT_MASK)
  1503. scorpion_clearpmu(hwc->config_base);
  1504. /* Disable interrupt for this counter */
  1505. armv7_pmnc_disable_intens(idx);
  1506. }
  1507. static void scorpion_pmu_enable_event(struct perf_event *event)
  1508. {
  1509. struct hw_perf_event *hwc = &event->hw;
  1510. int idx = hwc->idx;
  1511. /*
  1512. * Set event (if destined for PMNx counters)
  1513. * We don't set the event for the cycle counter because we
  1514. * don't have the ability to perform event filtering.
  1515. */
  1516. if (hwc->config_base & KRAIT_EVENT_MASK)
  1517. scorpion_evt_setup(idx, hwc->config_base);
  1518. else if (idx != ARMV7_IDX_CYCLE_COUNTER)
  1519. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1520. armv7_pmnc_enable_intens(idx);
  1521. armv7_pmnc_enable_counter(idx);
  1522. }
  1523. static void scorpion_pmu_reset(void *info)
  1524. {
  1525. u32 vval, fval;
  1526. struct arm_pmu *cpu_pmu = info;
  1527. u32 idx;
  1528. armv7pmu_reset(info);
  1529. /* Clear all pmresrs */
  1530. scorpion_write_pmresrn(0, 0);
  1531. scorpion_write_pmresrn(1, 0);
  1532. scorpion_write_pmresrn(2, 0);
  1533. scorpion_write_pmresrn(3, 0);
  1534. venum_pre_pmresr(&vval, &fval);
  1535. venum_write_pmresr(0);
  1536. venum_post_pmresr(vval, fval);
  1537. /* Reset PMxEVNCTCR to sane default */
  1538. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX) {
  1539. armv7_pmnc_select_counter(idx);
  1540. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1541. }
  1542. }
  1543. static int scorpion_event_to_bit(struct perf_event *event, unsigned int region,
  1544. unsigned int group)
  1545. {
  1546. int bit;
  1547. struct hw_perf_event *hwc = &event->hw;
  1548. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1549. if (hwc->config_base & VENUM_EVENT)
  1550. bit = SCORPION_VLPM_GROUP0;
  1551. else
  1552. bit = scorpion_get_pmresrn_event(region);
  1553. bit -= scorpion_get_pmresrn_event(0);
  1554. bit += group;
  1555. /*
  1556. * Lower bits are reserved for use by the counters (see
  1557. * armv7pmu_get_event_idx() for more info)
  1558. */
  1559. bit += bitmap_weight(cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX);
  1560. return bit;
  1561. }
  1562. /*
  1563. * We check for column exclusion constraints here.
  1564. * Two events cant use the same group within a pmresr register.
  1565. */
  1566. static int scorpion_pmu_get_event_idx(struct pmu_hw_events *cpuc,
  1567. struct perf_event *event)
  1568. {
  1569. int idx;
  1570. int bit = -1;
  1571. struct hw_perf_event *hwc = &event->hw;
  1572. unsigned int region = EVENT_REGION(hwc->config_base);
  1573. unsigned int group = EVENT_GROUP(hwc->config_base);
  1574. bool venum_event = EVENT_VENUM(hwc->config_base);
  1575. bool scorpion_event = EVENT_CPU(hwc->config_base);
  1576. if (venum_event || scorpion_event) {
  1577. /* Ignore invalid events */
  1578. if (group > 3 || region > 3)
  1579. return -EINVAL;
  1580. bit = scorpion_event_to_bit(event, region, group);
  1581. if (test_and_set_bit(bit, cpuc->used_mask))
  1582. return -EAGAIN;
  1583. }
  1584. idx = armv7pmu_get_event_idx(cpuc, event);
  1585. if (idx < 0 && bit >= 0)
  1586. clear_bit(bit, cpuc->used_mask);
  1587. return idx;
  1588. }
  1589. static void scorpion_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  1590. struct perf_event *event)
  1591. {
  1592. int bit;
  1593. struct hw_perf_event *hwc = &event->hw;
  1594. unsigned int region = EVENT_REGION(hwc->config_base);
  1595. unsigned int group = EVENT_GROUP(hwc->config_base);
  1596. bool venum_event = EVENT_VENUM(hwc->config_base);
  1597. bool scorpion_event = EVENT_CPU(hwc->config_base);
  1598. armv7pmu_clear_event_idx(cpuc, event);
  1599. if (venum_event || scorpion_event) {
  1600. bit = scorpion_event_to_bit(event, region, group);
  1601. clear_bit(bit, cpuc->used_mask);
  1602. }
  1603. }
  1604. static int scorpion_pmu_init(struct arm_pmu *cpu_pmu)
  1605. {
  1606. armv7pmu_init(cpu_pmu);
  1607. cpu_pmu->name = "armv7_scorpion";
  1608. cpu_pmu->map_event = scorpion_map_event;
  1609. cpu_pmu->reset = scorpion_pmu_reset;
  1610. cpu_pmu->enable = scorpion_pmu_enable_event;
  1611. cpu_pmu->disable = scorpion_pmu_disable_event;
  1612. cpu_pmu->get_event_idx = scorpion_pmu_get_event_idx;
  1613. cpu_pmu->clear_event_idx = scorpion_pmu_clear_event_idx;
  1614. return armv7_probe_num_events(cpu_pmu);
  1615. }
  1616. static int scorpion_mp_pmu_init(struct arm_pmu *cpu_pmu)
  1617. {
  1618. armv7pmu_init(cpu_pmu);
  1619. cpu_pmu->name = "armv7_scorpion_mp";
  1620. cpu_pmu->map_event = scorpion_map_event;
  1621. cpu_pmu->reset = scorpion_pmu_reset;
  1622. cpu_pmu->enable = scorpion_pmu_enable_event;
  1623. cpu_pmu->disable = scorpion_pmu_disable_event;
  1624. cpu_pmu->get_event_idx = scorpion_pmu_get_event_idx;
  1625. cpu_pmu->clear_event_idx = scorpion_pmu_clear_event_idx;
  1626. return armv7_probe_num_events(cpu_pmu);
  1627. }
  1628. static const struct of_device_id armv7_pmu_of_device_ids[] = {
  1629. {.compatible = "arm,cortex-a17-pmu", .data = armv7_a17_pmu_init},
  1630. {.compatible = "arm,cortex-a15-pmu", .data = armv7_a15_pmu_init},
  1631. {.compatible = "arm,cortex-a12-pmu", .data = armv7_a12_pmu_init},
  1632. {.compatible = "arm,cortex-a9-pmu", .data = armv7_a9_pmu_init},
  1633. {.compatible = "arm,cortex-a8-pmu", .data = armv7_a8_pmu_init},
  1634. {.compatible = "arm,cortex-a7-pmu", .data = armv7_a7_pmu_init},
  1635. {.compatible = "arm,cortex-a5-pmu", .data = armv7_a5_pmu_init},
  1636. {.compatible = "qcom,krait-pmu", .data = krait_pmu_init},
  1637. {.compatible = "qcom,scorpion-pmu", .data = scorpion_pmu_init},
  1638. {.compatible = "qcom,scorpion-mp-pmu", .data = scorpion_mp_pmu_init},
  1639. {},
  1640. };
  1641. static int armv7_pmu_device_probe(struct platform_device *pdev)
  1642. {
  1643. return arm_pmu_device_probe(pdev, armv7_pmu_of_device_ids, NULL);
  1644. }
  1645. static struct platform_driver armv7_pmu_driver = {
  1646. .driver = {
  1647. .name = "armv7-pmu",
  1648. .of_match_table = armv7_pmu_of_device_ids,
  1649. .suppress_bind_attrs = true,
  1650. },
  1651. .probe = armv7_pmu_device_probe,
  1652. };
  1653. builtin_platform_driver(armv7_pmu_driver);