perf_event_v7.c 64 KB

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