arm_v7_pmu.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  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. * Enable counter and interrupt, and set the counter to count
  734. * the event that we're interested in.
  735. */
  736. /*
  737. * Disable counter
  738. */
  739. armv7_pmnc_disable_counter(idx);
  740. /*
  741. * Set event (if destined for PMNx counters)
  742. * We only need to set the event for the cycle counter if we
  743. * have the ability to perform event filtering.
  744. */
  745. if (cpu_pmu->set_event_filter || idx != ARMV7_IDX_CYCLE_COUNTER)
  746. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  747. /*
  748. * Enable interrupt for this counter
  749. */
  750. armv7_pmnc_enable_intens(idx);
  751. /*
  752. * Enable counter
  753. */
  754. armv7_pmnc_enable_counter(idx);
  755. }
  756. static void armv7pmu_disable_event(struct perf_event *event)
  757. {
  758. struct hw_perf_event *hwc = &event->hw;
  759. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  760. int idx = hwc->idx;
  761. if (!armv7_pmnc_counter_valid(cpu_pmu, idx)) {
  762. pr_err("CPU%u disabling wrong PMNC counter IRQ enable %d\n",
  763. smp_processor_id(), idx);
  764. return;
  765. }
  766. /*
  767. * Disable counter and interrupt
  768. */
  769. /*
  770. * Disable counter
  771. */
  772. armv7_pmnc_disable_counter(idx);
  773. /*
  774. * Disable interrupt for this counter
  775. */
  776. armv7_pmnc_disable_intens(idx);
  777. }
  778. static irqreturn_t armv7pmu_handle_irq(struct arm_pmu *cpu_pmu)
  779. {
  780. u32 pmnc;
  781. struct perf_sample_data data;
  782. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  783. struct pt_regs *regs;
  784. int idx;
  785. /*
  786. * Get and reset the IRQ flags
  787. */
  788. pmnc = armv7_pmnc_getreset_flags();
  789. /*
  790. * Did an overflow occur?
  791. */
  792. if (!armv7_pmnc_has_overflowed(pmnc))
  793. return IRQ_NONE;
  794. /*
  795. * Handle the counter(s) overflow(s)
  796. */
  797. regs = get_irq_regs();
  798. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
  799. struct perf_event *event = cpuc->events[idx];
  800. struct hw_perf_event *hwc;
  801. /* Ignore if we don't have an event. */
  802. if (!event)
  803. continue;
  804. /*
  805. * We have a single interrupt for all counters. Check that
  806. * each counter has overflowed before we process it.
  807. */
  808. if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
  809. continue;
  810. hwc = &event->hw;
  811. armpmu_event_update(event);
  812. perf_sample_data_init(&data, 0, hwc->last_period);
  813. if (!armpmu_event_set_period(event))
  814. continue;
  815. if (perf_event_overflow(event, &data, regs))
  816. cpu_pmu->disable(event);
  817. }
  818. /*
  819. * Handle the pending perf events.
  820. *
  821. * Note: this call *must* be run with interrupts disabled. For
  822. * platforms that can have the PMU interrupts raised as an NMI, this
  823. * will not work.
  824. */
  825. irq_work_run();
  826. return IRQ_HANDLED;
  827. }
  828. static void armv7pmu_start(struct arm_pmu *cpu_pmu)
  829. {
  830. /* Enable all counters */
  831. armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
  832. }
  833. static void armv7pmu_stop(struct arm_pmu *cpu_pmu)
  834. {
  835. /* Disable all counters */
  836. armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
  837. }
  838. static int armv7pmu_get_event_idx(struct pmu_hw_events *cpuc,
  839. struct perf_event *event)
  840. {
  841. int idx;
  842. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  843. struct hw_perf_event *hwc = &event->hw;
  844. unsigned long evtype = hwc->config_base & ARMV7_EVTYPE_EVENT;
  845. /* Always place a cycle counter into the cycle counter. */
  846. if (evtype == ARMV7_PERFCTR_CPU_CYCLES) {
  847. if (test_and_set_bit(ARMV7_IDX_CYCLE_COUNTER, cpuc->used_mask))
  848. return -EAGAIN;
  849. return ARMV7_IDX_CYCLE_COUNTER;
  850. }
  851. /*
  852. * For anything other than a cycle counter, try and use
  853. * the events counters
  854. */
  855. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX) {
  856. if (!test_and_set_bit(idx, cpuc->used_mask))
  857. return idx;
  858. }
  859. /* The counters are all in use. */
  860. return -EAGAIN;
  861. }
  862. static void armv7pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  863. struct perf_event *event)
  864. {
  865. clear_bit(event->hw.idx, cpuc->used_mask);
  866. }
  867. /*
  868. * Add an event filter to a given event. This will only work for PMUv2 PMUs.
  869. */
  870. static int armv7pmu_set_event_filter(struct hw_perf_event *event,
  871. struct perf_event_attr *attr)
  872. {
  873. unsigned long config_base = 0;
  874. if (attr->exclude_idle) {
  875. pr_debug("ARM performance counters do not support mode exclusion\n");
  876. return -EOPNOTSUPP;
  877. }
  878. if (attr->exclude_user)
  879. config_base |= ARMV7_EXCLUDE_USER;
  880. if (attr->exclude_kernel)
  881. config_base |= ARMV7_EXCLUDE_PL1;
  882. if (!attr->exclude_hv)
  883. config_base |= ARMV7_INCLUDE_HYP;
  884. /*
  885. * Install the filter into config_base as this is used to
  886. * construct the event type.
  887. */
  888. event->config_base = config_base;
  889. return 0;
  890. }
  891. static void armv7pmu_reset(void *info)
  892. {
  893. struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
  894. u32 idx, val;
  895. if (cpu_pmu->secure_access) {
  896. asm volatile("mrc p15, 0, %0, c1, c1, 1" : "=r" (val));
  897. val |= ARMV7_SDER_SUNIDEN;
  898. asm volatile("mcr p15, 0, %0, c1, c1, 1" : : "r" (val));
  899. }
  900. /* The counter and interrupt enable registers are unknown at reset. */
  901. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
  902. armv7_pmnc_disable_counter(idx);
  903. armv7_pmnc_disable_intens(idx);
  904. }
  905. /* Initialize & Reset PMNC: C and P bits */
  906. armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
  907. }
  908. static int armv7_a8_map_event(struct perf_event *event)
  909. {
  910. return armpmu_map_event(event, &armv7_a8_perf_map,
  911. &armv7_a8_perf_cache_map, 0xFF);
  912. }
  913. static int armv7_a9_map_event(struct perf_event *event)
  914. {
  915. return armpmu_map_event(event, &armv7_a9_perf_map,
  916. &armv7_a9_perf_cache_map, 0xFF);
  917. }
  918. static int armv7_a5_map_event(struct perf_event *event)
  919. {
  920. return armpmu_map_event(event, &armv7_a5_perf_map,
  921. &armv7_a5_perf_cache_map, 0xFF);
  922. }
  923. static int armv7_a15_map_event(struct perf_event *event)
  924. {
  925. return armpmu_map_event(event, &armv7_a15_perf_map,
  926. &armv7_a15_perf_cache_map, 0xFF);
  927. }
  928. static int armv7_a7_map_event(struct perf_event *event)
  929. {
  930. return armpmu_map_event(event, &armv7_a7_perf_map,
  931. &armv7_a7_perf_cache_map, 0xFF);
  932. }
  933. static int armv7_a12_map_event(struct perf_event *event)
  934. {
  935. return armpmu_map_event(event, &armv7_a12_perf_map,
  936. &armv7_a12_perf_cache_map, 0xFF);
  937. }
  938. static int krait_map_event(struct perf_event *event)
  939. {
  940. return armpmu_map_event(event, &krait_perf_map,
  941. &krait_perf_cache_map, 0xFFFFF);
  942. }
  943. static int krait_map_event_no_branch(struct perf_event *event)
  944. {
  945. return armpmu_map_event(event, &krait_perf_map_no_branch,
  946. &krait_perf_cache_map, 0xFFFFF);
  947. }
  948. static int scorpion_map_event(struct perf_event *event)
  949. {
  950. return armpmu_map_event(event, &scorpion_perf_map,
  951. &scorpion_perf_cache_map, 0xFFFFF);
  952. }
  953. static void armv7pmu_init(struct arm_pmu *cpu_pmu)
  954. {
  955. cpu_pmu->handle_irq = armv7pmu_handle_irq;
  956. cpu_pmu->enable = armv7pmu_enable_event;
  957. cpu_pmu->disable = armv7pmu_disable_event;
  958. cpu_pmu->read_counter = armv7pmu_read_counter;
  959. cpu_pmu->write_counter = armv7pmu_write_counter;
  960. cpu_pmu->get_event_idx = armv7pmu_get_event_idx;
  961. cpu_pmu->clear_event_idx = armv7pmu_clear_event_idx;
  962. cpu_pmu->start = armv7pmu_start;
  963. cpu_pmu->stop = armv7pmu_stop;
  964. cpu_pmu->reset = armv7pmu_reset;
  965. };
  966. static void armv7_read_num_pmnc_events(void *info)
  967. {
  968. int nb_cnt;
  969. struct arm_pmu *cpu_pmu = info;
  970. /* Read the nb of CNTx counters supported from PMNC */
  971. nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
  972. bitmap_set(cpu_pmu->cntr_mask, 0, nb_cnt);
  973. /* Add the CPU cycles counter */
  974. set_bit(ARMV7_IDX_CYCLE_COUNTER, cpu_pmu->cntr_mask);
  975. }
  976. static int armv7_probe_num_events(struct arm_pmu *arm_pmu)
  977. {
  978. return smp_call_function_any(&arm_pmu->supported_cpus,
  979. armv7_read_num_pmnc_events,
  980. arm_pmu, 1);
  981. }
  982. static int armv7_a8_pmu_init(struct arm_pmu *cpu_pmu)
  983. {
  984. armv7pmu_init(cpu_pmu);
  985. cpu_pmu->name = "armv7_cortex_a8";
  986. cpu_pmu->map_event = armv7_a8_map_event;
  987. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  988. &armv7_pmuv1_events_attr_group;
  989. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  990. &armv7_pmu_format_attr_group;
  991. return armv7_probe_num_events(cpu_pmu);
  992. }
  993. static int armv7_a9_pmu_init(struct arm_pmu *cpu_pmu)
  994. {
  995. armv7pmu_init(cpu_pmu);
  996. cpu_pmu->name = "armv7_cortex_a9";
  997. cpu_pmu->map_event = armv7_a9_map_event;
  998. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  999. &armv7_pmuv1_events_attr_group;
  1000. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  1001. &armv7_pmu_format_attr_group;
  1002. return armv7_probe_num_events(cpu_pmu);
  1003. }
  1004. static int armv7_a5_pmu_init(struct arm_pmu *cpu_pmu)
  1005. {
  1006. armv7pmu_init(cpu_pmu);
  1007. cpu_pmu->name = "armv7_cortex_a5";
  1008. cpu_pmu->map_event = armv7_a5_map_event;
  1009. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  1010. &armv7_pmuv1_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_a15_pmu_init(struct arm_pmu *cpu_pmu)
  1016. {
  1017. armv7pmu_init(cpu_pmu);
  1018. cpu_pmu->name = "armv7_cortex_a15";
  1019. cpu_pmu->map_event = armv7_a15_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_a7_pmu_init(struct arm_pmu *cpu_pmu)
  1028. {
  1029. armv7pmu_init(cpu_pmu);
  1030. cpu_pmu->name = "armv7_cortex_a7";
  1031. cpu_pmu->map_event = armv7_a7_map_event;
  1032. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1033. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  1034. &armv7_pmuv2_events_attr_group;
  1035. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  1036. &armv7_pmu_format_attr_group;
  1037. return armv7_probe_num_events(cpu_pmu);
  1038. }
  1039. static int armv7_a12_pmu_init(struct arm_pmu *cpu_pmu)
  1040. {
  1041. armv7pmu_init(cpu_pmu);
  1042. cpu_pmu->name = "armv7_cortex_a12";
  1043. cpu_pmu->map_event = armv7_a12_map_event;
  1044. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1045. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  1046. &armv7_pmuv2_events_attr_group;
  1047. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  1048. &armv7_pmu_format_attr_group;
  1049. return armv7_probe_num_events(cpu_pmu);
  1050. }
  1051. static int armv7_a17_pmu_init(struct arm_pmu *cpu_pmu)
  1052. {
  1053. int ret = armv7_a12_pmu_init(cpu_pmu);
  1054. cpu_pmu->name = "armv7_cortex_a17";
  1055. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
  1056. &armv7_pmuv2_events_attr_group;
  1057. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
  1058. &armv7_pmu_format_attr_group;
  1059. return ret;
  1060. }
  1061. /*
  1062. * Krait Performance Monitor Region Event Selection Register (PMRESRn)
  1063. *
  1064. * 31 30 24 16 8 0
  1065. * +--------------------------------+
  1066. * PMRESR0 | EN | CC | CC | CC | CC | N = 1, R = 0
  1067. * +--------------------------------+
  1068. * PMRESR1 | EN | CC | CC | CC | CC | N = 1, R = 1
  1069. * +--------------------------------+
  1070. * PMRESR2 | EN | CC | CC | CC | CC | N = 1, R = 2
  1071. * +--------------------------------+
  1072. * VPMRESR0 | EN | CC | CC | CC | CC | N = 2, R = ?
  1073. * +--------------------------------+
  1074. * EN | G=3 | G=2 | G=1 | G=0
  1075. *
  1076. * Event Encoding:
  1077. *
  1078. * hwc->config_base = 0xNRCCG
  1079. *
  1080. * N = prefix, 1 for Krait CPU (PMRESRn), 2 for Venum VFP (VPMRESR)
  1081. * R = region register
  1082. * CC = class of events the group G is choosing from
  1083. * G = group or particular event
  1084. *
  1085. * Example: 0x12021 is a Krait CPU event in PMRESR2's group 1 with code 2
  1086. *
  1087. * A region (R) corresponds to a piece of the CPU (execution unit, instruction
  1088. * unit, etc.) while the event code (CC) corresponds to a particular class of
  1089. * events (interrupts for example). An event code is broken down into
  1090. * groups (G) that can be mapped into the PMU (irq, fiqs, and irq+fiqs for
  1091. * example).
  1092. */
  1093. #define KRAIT_EVENT (1 << 16)
  1094. #define VENUM_EVENT (2 << 16)
  1095. #define KRAIT_EVENT_MASK (KRAIT_EVENT | VENUM_EVENT)
  1096. #define PMRESRn_EN BIT(31)
  1097. #define EVENT_REGION(event) (((event) >> 12) & 0xf) /* R */
  1098. #define EVENT_GROUP(event) ((event) & 0xf) /* G */
  1099. #define EVENT_CODE(event) (((event) >> 4) & 0xff) /* CC */
  1100. #define EVENT_VENUM(event) (!!(event & VENUM_EVENT)) /* N=2 */
  1101. #define EVENT_CPU(event) (!!(event & KRAIT_EVENT)) /* N=1 */
  1102. static u32 krait_read_pmresrn(int n)
  1103. {
  1104. u32 val;
  1105. switch (n) {
  1106. case 0:
  1107. asm volatile("mrc p15, 1, %0, c9, c15, 0" : "=r" (val));
  1108. break;
  1109. case 1:
  1110. asm volatile("mrc p15, 1, %0, c9, c15, 1" : "=r" (val));
  1111. break;
  1112. case 2:
  1113. asm volatile("mrc p15, 1, %0, c9, c15, 2" : "=r" (val));
  1114. break;
  1115. default:
  1116. BUG(); /* Should be validated in krait_pmu_get_event_idx() */
  1117. }
  1118. return val;
  1119. }
  1120. static void krait_write_pmresrn(int n, u32 val)
  1121. {
  1122. switch (n) {
  1123. case 0:
  1124. asm volatile("mcr p15, 1, %0, c9, c15, 0" : : "r" (val));
  1125. break;
  1126. case 1:
  1127. asm volatile("mcr p15, 1, %0, c9, c15, 1" : : "r" (val));
  1128. break;
  1129. case 2:
  1130. asm volatile("mcr p15, 1, %0, c9, c15, 2" : : "r" (val));
  1131. break;
  1132. default:
  1133. BUG(); /* Should be validated in krait_pmu_get_event_idx() */
  1134. }
  1135. }
  1136. static u32 venum_read_pmresr(void)
  1137. {
  1138. u32 val;
  1139. asm volatile("mrc p10, 7, %0, c11, c0, 0" : "=r" (val));
  1140. return val;
  1141. }
  1142. static void venum_write_pmresr(u32 val)
  1143. {
  1144. asm volatile("mcr p10, 7, %0, c11, c0, 0" : : "r" (val));
  1145. }
  1146. static void venum_pre_pmresr(u32 *venum_orig_val, u32 *fp_orig_val)
  1147. {
  1148. u32 venum_new_val;
  1149. u32 fp_new_val;
  1150. BUG_ON(preemptible());
  1151. /* CPACR Enable CP10 and CP11 access */
  1152. *venum_orig_val = get_copro_access();
  1153. venum_new_val = *venum_orig_val | CPACC_SVC(10) | CPACC_SVC(11);
  1154. set_copro_access(venum_new_val);
  1155. /* Enable FPEXC */
  1156. *fp_orig_val = fmrx(FPEXC);
  1157. fp_new_val = *fp_orig_val | FPEXC_EN;
  1158. fmxr(FPEXC, fp_new_val);
  1159. }
  1160. static void venum_post_pmresr(u32 venum_orig_val, u32 fp_orig_val)
  1161. {
  1162. BUG_ON(preemptible());
  1163. /* Restore FPEXC */
  1164. fmxr(FPEXC, fp_orig_val);
  1165. isb();
  1166. /* Restore CPACR */
  1167. set_copro_access(venum_orig_val);
  1168. }
  1169. static u32 krait_get_pmresrn_event(unsigned int region)
  1170. {
  1171. static const u32 pmresrn_table[] = { KRAIT_PMRESR0_GROUP0,
  1172. KRAIT_PMRESR1_GROUP0,
  1173. KRAIT_PMRESR2_GROUP0 };
  1174. return pmresrn_table[region];
  1175. }
  1176. static void krait_evt_setup(int idx, u32 config_base)
  1177. {
  1178. u32 val;
  1179. u32 mask;
  1180. u32 vval, fval;
  1181. unsigned int region = EVENT_REGION(config_base);
  1182. unsigned int group = EVENT_GROUP(config_base);
  1183. unsigned int code = EVENT_CODE(config_base);
  1184. unsigned int group_shift;
  1185. bool venum_event = EVENT_VENUM(config_base);
  1186. group_shift = group * 8;
  1187. mask = 0xff << group_shift;
  1188. /* Configure evtsel for the region and group */
  1189. if (venum_event)
  1190. val = KRAIT_VPMRESR0_GROUP0;
  1191. else
  1192. val = krait_get_pmresrn_event(region);
  1193. val += group;
  1194. /* Mix in mode-exclusion bits */
  1195. val |= config_base & (ARMV7_EXCLUDE_USER | ARMV7_EXCLUDE_PL1);
  1196. armv7_pmnc_write_evtsel(idx, val);
  1197. if (venum_event) {
  1198. venum_pre_pmresr(&vval, &fval);
  1199. val = venum_read_pmresr();
  1200. val &= ~mask;
  1201. val |= code << group_shift;
  1202. val |= PMRESRn_EN;
  1203. venum_write_pmresr(val);
  1204. venum_post_pmresr(vval, fval);
  1205. } else {
  1206. val = krait_read_pmresrn(region);
  1207. val &= ~mask;
  1208. val |= code << group_shift;
  1209. val |= PMRESRn_EN;
  1210. krait_write_pmresrn(region, val);
  1211. }
  1212. }
  1213. static u32 clear_pmresrn_group(u32 val, int group)
  1214. {
  1215. u32 mask;
  1216. int group_shift;
  1217. group_shift = group * 8;
  1218. mask = 0xff << group_shift;
  1219. val &= ~mask;
  1220. /* Don't clear enable bit if entire region isn't disabled */
  1221. if (val & ~PMRESRn_EN)
  1222. return val |= PMRESRn_EN;
  1223. return 0;
  1224. }
  1225. static void krait_clearpmu(u32 config_base)
  1226. {
  1227. u32 val;
  1228. u32 vval, fval;
  1229. unsigned int region = EVENT_REGION(config_base);
  1230. unsigned int group = EVENT_GROUP(config_base);
  1231. bool venum_event = EVENT_VENUM(config_base);
  1232. if (venum_event) {
  1233. venum_pre_pmresr(&vval, &fval);
  1234. val = venum_read_pmresr();
  1235. val = clear_pmresrn_group(val, group);
  1236. venum_write_pmresr(val);
  1237. venum_post_pmresr(vval, fval);
  1238. } else {
  1239. val = krait_read_pmresrn(region);
  1240. val = clear_pmresrn_group(val, group);
  1241. krait_write_pmresrn(region, val);
  1242. }
  1243. }
  1244. static void krait_pmu_disable_event(struct perf_event *event)
  1245. {
  1246. struct hw_perf_event *hwc = &event->hw;
  1247. int idx = hwc->idx;
  1248. /* Disable counter and interrupt */
  1249. /* Disable counter */
  1250. armv7_pmnc_disable_counter(idx);
  1251. /*
  1252. * Clear pmresr code (if destined for PMNx counters)
  1253. */
  1254. if (hwc->config_base & KRAIT_EVENT_MASK)
  1255. krait_clearpmu(hwc->config_base);
  1256. /* Disable interrupt for this counter */
  1257. armv7_pmnc_disable_intens(idx);
  1258. }
  1259. static void krait_pmu_enable_event(struct perf_event *event)
  1260. {
  1261. struct hw_perf_event *hwc = &event->hw;
  1262. int idx = hwc->idx;
  1263. /*
  1264. * Enable counter and interrupt, and set the counter to count
  1265. * the event that we're interested in.
  1266. */
  1267. /* Disable counter */
  1268. armv7_pmnc_disable_counter(idx);
  1269. /*
  1270. * Set event (if destined for PMNx counters)
  1271. * We set the event for the cycle counter because we
  1272. * have the ability to perform event filtering.
  1273. */
  1274. if (hwc->config_base & KRAIT_EVENT_MASK)
  1275. krait_evt_setup(idx, hwc->config_base);
  1276. else
  1277. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1278. /* Enable interrupt for this counter */
  1279. armv7_pmnc_enable_intens(idx);
  1280. /* Enable counter */
  1281. armv7_pmnc_enable_counter(idx);
  1282. }
  1283. static void krait_pmu_reset(void *info)
  1284. {
  1285. u32 vval, fval;
  1286. struct arm_pmu *cpu_pmu = info;
  1287. u32 idx;
  1288. armv7pmu_reset(info);
  1289. /* Clear all pmresrs */
  1290. krait_write_pmresrn(0, 0);
  1291. krait_write_pmresrn(1, 0);
  1292. krait_write_pmresrn(2, 0);
  1293. venum_pre_pmresr(&vval, &fval);
  1294. venum_write_pmresr(0);
  1295. venum_post_pmresr(vval, fval);
  1296. /* Reset PMxEVNCTCR to sane default */
  1297. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX) {
  1298. armv7_pmnc_select_counter(idx);
  1299. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1300. }
  1301. }
  1302. static int krait_event_to_bit(struct perf_event *event, unsigned int region,
  1303. unsigned int group)
  1304. {
  1305. int bit;
  1306. struct hw_perf_event *hwc = &event->hw;
  1307. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1308. if (hwc->config_base & VENUM_EVENT)
  1309. bit = KRAIT_VPMRESR0_GROUP0;
  1310. else
  1311. bit = krait_get_pmresrn_event(region);
  1312. bit -= krait_get_pmresrn_event(0);
  1313. bit += group;
  1314. /*
  1315. * Lower bits are reserved for use by the counters (see
  1316. * armv7pmu_get_event_idx() for more info)
  1317. */
  1318. bit += bitmap_weight(cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX);
  1319. return bit;
  1320. }
  1321. /*
  1322. * We check for column exclusion constraints here.
  1323. * Two events cant use the same group within a pmresr register.
  1324. */
  1325. static int krait_pmu_get_event_idx(struct pmu_hw_events *cpuc,
  1326. struct perf_event *event)
  1327. {
  1328. int idx;
  1329. int bit = -1;
  1330. struct hw_perf_event *hwc = &event->hw;
  1331. unsigned int region = EVENT_REGION(hwc->config_base);
  1332. unsigned int code = EVENT_CODE(hwc->config_base);
  1333. unsigned int group = EVENT_GROUP(hwc->config_base);
  1334. bool venum_event = EVENT_VENUM(hwc->config_base);
  1335. bool krait_event = EVENT_CPU(hwc->config_base);
  1336. if (venum_event || krait_event) {
  1337. /* Ignore invalid events */
  1338. if (group > 3 || region > 2)
  1339. return -EINVAL;
  1340. if (venum_event && (code & 0xe0))
  1341. return -EINVAL;
  1342. bit = krait_event_to_bit(event, region, group);
  1343. if (test_and_set_bit(bit, cpuc->used_mask))
  1344. return -EAGAIN;
  1345. }
  1346. idx = armv7pmu_get_event_idx(cpuc, event);
  1347. if (idx < 0 && bit >= 0)
  1348. clear_bit(bit, cpuc->used_mask);
  1349. return idx;
  1350. }
  1351. static void krait_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  1352. struct perf_event *event)
  1353. {
  1354. int bit;
  1355. struct hw_perf_event *hwc = &event->hw;
  1356. unsigned int region = EVENT_REGION(hwc->config_base);
  1357. unsigned int group = EVENT_GROUP(hwc->config_base);
  1358. bool venum_event = EVENT_VENUM(hwc->config_base);
  1359. bool krait_event = EVENT_CPU(hwc->config_base);
  1360. armv7pmu_clear_event_idx(cpuc, event);
  1361. if (venum_event || krait_event) {
  1362. bit = krait_event_to_bit(event, region, group);
  1363. clear_bit(bit, cpuc->used_mask);
  1364. }
  1365. }
  1366. static int krait_pmu_init(struct arm_pmu *cpu_pmu)
  1367. {
  1368. armv7pmu_init(cpu_pmu);
  1369. cpu_pmu->name = "armv7_krait";
  1370. /* Some early versions of Krait don't support PC write events */
  1371. if (of_property_read_bool(cpu_pmu->plat_device->dev.of_node,
  1372. "qcom,no-pc-write"))
  1373. cpu_pmu->map_event = krait_map_event_no_branch;
  1374. else
  1375. cpu_pmu->map_event = krait_map_event;
  1376. cpu_pmu->set_event_filter = armv7pmu_set_event_filter;
  1377. cpu_pmu->reset = krait_pmu_reset;
  1378. cpu_pmu->enable = krait_pmu_enable_event;
  1379. cpu_pmu->disable = krait_pmu_disable_event;
  1380. cpu_pmu->get_event_idx = krait_pmu_get_event_idx;
  1381. cpu_pmu->clear_event_idx = krait_pmu_clear_event_idx;
  1382. return armv7_probe_num_events(cpu_pmu);
  1383. }
  1384. /*
  1385. * Scorpion Local Performance Monitor Register (LPMn)
  1386. *
  1387. * 31 30 24 16 8 0
  1388. * +--------------------------------+
  1389. * LPM0 | EN | CC | CC | CC | CC | N = 1, R = 0
  1390. * +--------------------------------+
  1391. * LPM1 | EN | CC | CC | CC | CC | N = 1, R = 1
  1392. * +--------------------------------+
  1393. * LPM2 | EN | CC | CC | CC | CC | N = 1, R = 2
  1394. * +--------------------------------+
  1395. * L2LPM | EN | CC | CC | CC | CC | N = 1, R = 3
  1396. * +--------------------------------+
  1397. * VLPM | EN | CC | CC | CC | CC | N = 2, R = ?
  1398. * +--------------------------------+
  1399. * EN | G=3 | G=2 | G=1 | G=0
  1400. *
  1401. *
  1402. * Event Encoding:
  1403. *
  1404. * hwc->config_base = 0xNRCCG
  1405. *
  1406. * N = prefix, 1 for Scorpion CPU (LPMn/L2LPM), 2 for Venum VFP (VLPM)
  1407. * R = region register
  1408. * CC = class of events the group G is choosing from
  1409. * G = group or particular event
  1410. *
  1411. * Example: 0x12021 is a Scorpion CPU event in LPM2's group 1 with code 2
  1412. *
  1413. * A region (R) corresponds to a piece of the CPU (execution unit, instruction
  1414. * unit, etc.) while the event code (CC) corresponds to a particular class of
  1415. * events (interrupts for example). An event code is broken down into
  1416. * groups (G) that can be mapped into the PMU (irq, fiqs, and irq+fiqs for
  1417. * example).
  1418. */
  1419. static u32 scorpion_read_pmresrn(int n)
  1420. {
  1421. u32 val;
  1422. switch (n) {
  1423. case 0:
  1424. asm volatile("mrc p15, 0, %0, c15, c0, 0" : "=r" (val));
  1425. break;
  1426. case 1:
  1427. asm volatile("mrc p15, 1, %0, c15, c0, 0" : "=r" (val));
  1428. break;
  1429. case 2:
  1430. asm volatile("mrc p15, 2, %0, c15, c0, 0" : "=r" (val));
  1431. break;
  1432. case 3:
  1433. asm volatile("mrc p15, 3, %0, c15, c2, 0" : "=r" (val));
  1434. break;
  1435. default:
  1436. BUG(); /* Should be validated in scorpion_pmu_get_event_idx() */
  1437. }
  1438. return val;
  1439. }
  1440. static void scorpion_write_pmresrn(int n, u32 val)
  1441. {
  1442. switch (n) {
  1443. case 0:
  1444. asm volatile("mcr p15, 0, %0, c15, c0, 0" : : "r" (val));
  1445. break;
  1446. case 1:
  1447. asm volatile("mcr p15, 1, %0, c15, c0, 0" : : "r" (val));
  1448. break;
  1449. case 2:
  1450. asm volatile("mcr p15, 2, %0, c15, c0, 0" : : "r" (val));
  1451. break;
  1452. case 3:
  1453. asm volatile("mcr p15, 3, %0, c15, c2, 0" : : "r" (val));
  1454. break;
  1455. default:
  1456. BUG(); /* Should be validated in scorpion_pmu_get_event_idx() */
  1457. }
  1458. }
  1459. static u32 scorpion_get_pmresrn_event(unsigned int region)
  1460. {
  1461. static const u32 pmresrn_table[] = { SCORPION_LPM0_GROUP0,
  1462. SCORPION_LPM1_GROUP0,
  1463. SCORPION_LPM2_GROUP0,
  1464. SCORPION_L2LPM_GROUP0 };
  1465. return pmresrn_table[region];
  1466. }
  1467. static void scorpion_evt_setup(int idx, u32 config_base)
  1468. {
  1469. u32 val;
  1470. u32 mask;
  1471. u32 vval, fval;
  1472. unsigned int region = EVENT_REGION(config_base);
  1473. unsigned int group = EVENT_GROUP(config_base);
  1474. unsigned int code = EVENT_CODE(config_base);
  1475. unsigned int group_shift;
  1476. bool venum_event = EVENT_VENUM(config_base);
  1477. group_shift = group * 8;
  1478. mask = 0xff << group_shift;
  1479. /* Configure evtsel for the region and group */
  1480. if (venum_event)
  1481. val = SCORPION_VLPM_GROUP0;
  1482. else
  1483. val = scorpion_get_pmresrn_event(region);
  1484. val += group;
  1485. /* Mix in mode-exclusion bits */
  1486. val |= config_base & (ARMV7_EXCLUDE_USER | ARMV7_EXCLUDE_PL1);
  1487. armv7_pmnc_write_evtsel(idx, val);
  1488. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1489. if (venum_event) {
  1490. venum_pre_pmresr(&vval, &fval);
  1491. val = venum_read_pmresr();
  1492. val &= ~mask;
  1493. val |= code << group_shift;
  1494. val |= PMRESRn_EN;
  1495. venum_write_pmresr(val);
  1496. venum_post_pmresr(vval, fval);
  1497. } else {
  1498. val = scorpion_read_pmresrn(region);
  1499. val &= ~mask;
  1500. val |= code << group_shift;
  1501. val |= PMRESRn_EN;
  1502. scorpion_write_pmresrn(region, val);
  1503. }
  1504. }
  1505. static void scorpion_clearpmu(u32 config_base)
  1506. {
  1507. u32 val;
  1508. u32 vval, fval;
  1509. unsigned int region = EVENT_REGION(config_base);
  1510. unsigned int group = EVENT_GROUP(config_base);
  1511. bool venum_event = EVENT_VENUM(config_base);
  1512. if (venum_event) {
  1513. venum_pre_pmresr(&vval, &fval);
  1514. val = venum_read_pmresr();
  1515. val = clear_pmresrn_group(val, group);
  1516. venum_write_pmresr(val);
  1517. venum_post_pmresr(vval, fval);
  1518. } else {
  1519. val = scorpion_read_pmresrn(region);
  1520. val = clear_pmresrn_group(val, group);
  1521. scorpion_write_pmresrn(region, val);
  1522. }
  1523. }
  1524. static void scorpion_pmu_disable_event(struct perf_event *event)
  1525. {
  1526. struct hw_perf_event *hwc = &event->hw;
  1527. int idx = hwc->idx;
  1528. /* Disable counter and interrupt */
  1529. /* Disable counter */
  1530. armv7_pmnc_disable_counter(idx);
  1531. /*
  1532. * Clear pmresr code (if destined for PMNx counters)
  1533. */
  1534. if (hwc->config_base & KRAIT_EVENT_MASK)
  1535. scorpion_clearpmu(hwc->config_base);
  1536. /* Disable interrupt for this counter */
  1537. armv7_pmnc_disable_intens(idx);
  1538. }
  1539. static void scorpion_pmu_enable_event(struct perf_event *event)
  1540. {
  1541. struct hw_perf_event *hwc = &event->hw;
  1542. int idx = hwc->idx;
  1543. /*
  1544. * Enable counter and interrupt, and set the counter to count
  1545. * the event that we're interested in.
  1546. */
  1547. /* Disable counter */
  1548. armv7_pmnc_disable_counter(idx);
  1549. /*
  1550. * Set event (if destined for PMNx counters)
  1551. * We don't set the event for the cycle counter because we
  1552. * don't have the ability to perform event filtering.
  1553. */
  1554. if (hwc->config_base & KRAIT_EVENT_MASK)
  1555. scorpion_evt_setup(idx, hwc->config_base);
  1556. else if (idx != ARMV7_IDX_CYCLE_COUNTER)
  1557. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1558. /* Enable interrupt for this counter */
  1559. armv7_pmnc_enable_intens(idx);
  1560. /* Enable counter */
  1561. armv7_pmnc_enable_counter(idx);
  1562. }
  1563. static void scorpion_pmu_reset(void *info)
  1564. {
  1565. u32 vval, fval;
  1566. struct arm_pmu *cpu_pmu = info;
  1567. u32 idx;
  1568. armv7pmu_reset(info);
  1569. /* Clear all pmresrs */
  1570. scorpion_write_pmresrn(0, 0);
  1571. scorpion_write_pmresrn(1, 0);
  1572. scorpion_write_pmresrn(2, 0);
  1573. scorpion_write_pmresrn(3, 0);
  1574. venum_pre_pmresr(&vval, &fval);
  1575. venum_write_pmresr(0);
  1576. venum_post_pmresr(vval, fval);
  1577. /* Reset PMxEVNCTCR to sane default */
  1578. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX) {
  1579. armv7_pmnc_select_counter(idx);
  1580. asm volatile("mcr p15, 0, %0, c9, c15, 0" : : "r" (0));
  1581. }
  1582. }
  1583. static int scorpion_event_to_bit(struct perf_event *event, unsigned int region,
  1584. unsigned int group)
  1585. {
  1586. int bit;
  1587. struct hw_perf_event *hwc = &event->hw;
  1588. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  1589. if (hwc->config_base & VENUM_EVENT)
  1590. bit = SCORPION_VLPM_GROUP0;
  1591. else
  1592. bit = scorpion_get_pmresrn_event(region);
  1593. bit -= scorpion_get_pmresrn_event(0);
  1594. bit += group;
  1595. /*
  1596. * Lower bits are reserved for use by the counters (see
  1597. * armv7pmu_get_event_idx() for more info)
  1598. */
  1599. bit += bitmap_weight(cpu_pmu->cntr_mask, ARMV7_IDX_COUNTER_MAX);
  1600. return bit;
  1601. }
  1602. /*
  1603. * We check for column exclusion constraints here.
  1604. * Two events cant use the same group within a pmresr register.
  1605. */
  1606. static int scorpion_pmu_get_event_idx(struct pmu_hw_events *cpuc,
  1607. struct perf_event *event)
  1608. {
  1609. int idx;
  1610. int bit = -1;
  1611. struct hw_perf_event *hwc = &event->hw;
  1612. unsigned int region = EVENT_REGION(hwc->config_base);
  1613. unsigned int group = EVENT_GROUP(hwc->config_base);
  1614. bool venum_event = EVENT_VENUM(hwc->config_base);
  1615. bool scorpion_event = EVENT_CPU(hwc->config_base);
  1616. if (venum_event || scorpion_event) {
  1617. /* Ignore invalid events */
  1618. if (group > 3 || region > 3)
  1619. return -EINVAL;
  1620. bit = scorpion_event_to_bit(event, region, group);
  1621. if (test_and_set_bit(bit, cpuc->used_mask))
  1622. return -EAGAIN;
  1623. }
  1624. idx = armv7pmu_get_event_idx(cpuc, event);
  1625. if (idx < 0 && bit >= 0)
  1626. clear_bit(bit, cpuc->used_mask);
  1627. return idx;
  1628. }
  1629. static void scorpion_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  1630. struct perf_event *event)
  1631. {
  1632. int bit;
  1633. struct hw_perf_event *hwc = &event->hw;
  1634. unsigned int region = EVENT_REGION(hwc->config_base);
  1635. unsigned int group = EVENT_GROUP(hwc->config_base);
  1636. bool venum_event = EVENT_VENUM(hwc->config_base);
  1637. bool scorpion_event = EVENT_CPU(hwc->config_base);
  1638. armv7pmu_clear_event_idx(cpuc, event);
  1639. if (venum_event || scorpion_event) {
  1640. bit = scorpion_event_to_bit(event, region, group);
  1641. clear_bit(bit, cpuc->used_mask);
  1642. }
  1643. }
  1644. static int scorpion_pmu_init(struct arm_pmu *cpu_pmu)
  1645. {
  1646. armv7pmu_init(cpu_pmu);
  1647. cpu_pmu->name = "armv7_scorpion";
  1648. cpu_pmu->map_event = scorpion_map_event;
  1649. cpu_pmu->reset = scorpion_pmu_reset;
  1650. cpu_pmu->enable = scorpion_pmu_enable_event;
  1651. cpu_pmu->disable = scorpion_pmu_disable_event;
  1652. cpu_pmu->get_event_idx = scorpion_pmu_get_event_idx;
  1653. cpu_pmu->clear_event_idx = scorpion_pmu_clear_event_idx;
  1654. return armv7_probe_num_events(cpu_pmu);
  1655. }
  1656. static int scorpion_mp_pmu_init(struct arm_pmu *cpu_pmu)
  1657. {
  1658. armv7pmu_init(cpu_pmu);
  1659. cpu_pmu->name = "armv7_scorpion_mp";
  1660. cpu_pmu->map_event = scorpion_map_event;
  1661. cpu_pmu->reset = scorpion_pmu_reset;
  1662. cpu_pmu->enable = scorpion_pmu_enable_event;
  1663. cpu_pmu->disable = scorpion_pmu_disable_event;
  1664. cpu_pmu->get_event_idx = scorpion_pmu_get_event_idx;
  1665. cpu_pmu->clear_event_idx = scorpion_pmu_clear_event_idx;
  1666. return armv7_probe_num_events(cpu_pmu);
  1667. }
  1668. static const struct of_device_id armv7_pmu_of_device_ids[] = {
  1669. {.compatible = "arm,cortex-a17-pmu", .data = armv7_a17_pmu_init},
  1670. {.compatible = "arm,cortex-a15-pmu", .data = armv7_a15_pmu_init},
  1671. {.compatible = "arm,cortex-a12-pmu", .data = armv7_a12_pmu_init},
  1672. {.compatible = "arm,cortex-a9-pmu", .data = armv7_a9_pmu_init},
  1673. {.compatible = "arm,cortex-a8-pmu", .data = armv7_a8_pmu_init},
  1674. {.compatible = "arm,cortex-a7-pmu", .data = armv7_a7_pmu_init},
  1675. {.compatible = "arm,cortex-a5-pmu", .data = armv7_a5_pmu_init},
  1676. {.compatible = "qcom,krait-pmu", .data = krait_pmu_init},
  1677. {.compatible = "qcom,scorpion-pmu", .data = scorpion_pmu_init},
  1678. {.compatible = "qcom,scorpion-mp-pmu", .data = scorpion_mp_pmu_init},
  1679. {},
  1680. };
  1681. static int armv7_pmu_device_probe(struct platform_device *pdev)
  1682. {
  1683. return arm_pmu_device_probe(pdev, armv7_pmu_of_device_ids, NULL);
  1684. }
  1685. static struct platform_driver armv7_pmu_driver = {
  1686. .driver = {
  1687. .name = "armv7-pmu",
  1688. .of_match_table = armv7_pmu_of_device_ids,
  1689. .suppress_bind_attrs = true,
  1690. },
  1691. .probe = armv7_pmu_device_probe,
  1692. };
  1693. builtin_platform_driver(armv7_pmu_driver);