perf_event_v7.c 64 KB

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