arm-cci.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /*
  2. * CCI cache coherent interconnect driver
  3. *
  4. * Copyright (C) 2013 ARM Ltd.
  5. * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/arm-cci.h>
  17. #include <linux/io.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/perf_event.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/smp_plat.h>
  29. static void __iomem *cci_ctrl_base;
  30. static unsigned long cci_ctrl_phys;
  31. #ifdef CONFIG_ARM_CCI400_PORT_CTRL
  32. struct cci_nb_ports {
  33. unsigned int nb_ace;
  34. unsigned int nb_ace_lite;
  35. };
  36. static const struct cci_nb_ports cci400_ports = {
  37. .nb_ace = 2,
  38. .nb_ace_lite = 3
  39. };
  40. #define CCI400_PORTS_DATA (&cci400_ports)
  41. #else
  42. #define CCI400_PORTS_DATA (NULL)
  43. #endif
  44. static const struct of_device_id arm_cci_matches[] = {
  45. #ifdef CONFIG_ARM_CCI400_COMMON
  46. {.compatible = "arm,cci-400", .data = CCI400_PORTS_DATA },
  47. #endif
  48. #ifdef CONFIG_ARM_CCI500_PMU
  49. { .compatible = "arm,cci-500", },
  50. #endif
  51. {},
  52. };
  53. #ifdef CONFIG_ARM_CCI_PMU
  54. #define DRIVER_NAME "ARM-CCI"
  55. #define DRIVER_NAME_PMU DRIVER_NAME " PMU"
  56. #define CCI_PMCR 0x0100
  57. #define CCI_PID2 0x0fe8
  58. #define CCI_PMCR_CEN 0x00000001
  59. #define CCI_PMCR_NCNT_MASK 0x0000f800
  60. #define CCI_PMCR_NCNT_SHIFT 11
  61. #define CCI_PID2_REV_MASK 0xf0
  62. #define CCI_PID2_REV_SHIFT 4
  63. #define CCI_PMU_EVT_SEL 0x000
  64. #define CCI_PMU_CNTR 0x004
  65. #define CCI_PMU_CNTR_CTRL 0x008
  66. #define CCI_PMU_OVRFLW 0x00c
  67. #define CCI_PMU_OVRFLW_FLAG 1
  68. #define CCI_PMU_CNTR_SIZE(model) ((model)->cntr_size)
  69. #define CCI_PMU_CNTR_BASE(model, idx) ((idx) * CCI_PMU_CNTR_SIZE(model))
  70. #define CCI_PMU_CNTR_MASK ((1ULL << 32) -1)
  71. #define CCI_PMU_CNTR_LAST(cci_pmu) (cci_pmu->num_cntrs - 1)
  72. #define CCI_PMU_MAX_HW_CNTRS(model) \
  73. ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
  74. /* Types of interfaces that can generate events */
  75. enum {
  76. CCI_IF_SLAVE,
  77. CCI_IF_MASTER,
  78. #ifdef CONFIG_ARM_CCI500_PMU
  79. CCI_IF_GLOBAL,
  80. #endif
  81. CCI_IF_MAX,
  82. };
  83. struct event_range {
  84. u32 min;
  85. u32 max;
  86. };
  87. struct cci_pmu_hw_events {
  88. struct perf_event **events;
  89. unsigned long *used_mask;
  90. raw_spinlock_t pmu_lock;
  91. };
  92. struct cci_pmu;
  93. /*
  94. * struct cci_pmu_model:
  95. * @fixed_hw_cntrs - Number of fixed event counters
  96. * @num_hw_cntrs - Maximum number of programmable event counters
  97. * @cntr_size - Size of an event counter mapping
  98. */
  99. struct cci_pmu_model {
  100. char *name;
  101. u32 fixed_hw_cntrs;
  102. u32 num_hw_cntrs;
  103. u32 cntr_size;
  104. u64 nformat_attrs;
  105. u64 nevent_attrs;
  106. struct dev_ext_attribute *format_attrs;
  107. struct dev_ext_attribute *event_attrs;
  108. struct event_range event_ranges[CCI_IF_MAX];
  109. int (*validate_hw_event)(struct cci_pmu *, unsigned long);
  110. int (*get_event_idx)(struct cci_pmu *, struct cci_pmu_hw_events *, unsigned long);
  111. };
  112. static struct cci_pmu_model cci_pmu_models[];
  113. struct cci_pmu {
  114. void __iomem *base;
  115. struct pmu pmu;
  116. int nr_irqs;
  117. int *irqs;
  118. unsigned long active_irqs;
  119. const struct cci_pmu_model *model;
  120. struct cci_pmu_hw_events hw_events;
  121. struct platform_device *plat_device;
  122. int num_cntrs;
  123. atomic_t active_events;
  124. struct mutex reserve_mutex;
  125. struct notifier_block cpu_nb;
  126. cpumask_t cpus;
  127. };
  128. #define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
  129. enum cci_models {
  130. #ifdef CONFIG_ARM_CCI400_PMU
  131. CCI400_R0,
  132. CCI400_R1,
  133. #endif
  134. #ifdef CONFIG_ARM_CCI500_PMU
  135. CCI500_R0,
  136. #endif
  137. CCI_MODEL_MAX
  138. };
  139. static ssize_t cci_pmu_format_show(struct device *dev,
  140. struct device_attribute *attr, char *buf);
  141. static ssize_t cci_pmu_event_show(struct device *dev,
  142. struct device_attribute *attr, char *buf);
  143. #define CCI_EXT_ATTR_ENTRY(_name, _func, _config) \
  144. { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config }
  145. #define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
  146. CCI_EXT_ATTR_ENTRY(_name, cci_pmu_format_show, (char *)_config)
  147. #define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
  148. CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
  149. /* CCI400 PMU Specific definitions */
  150. #ifdef CONFIG_ARM_CCI400_PMU
  151. /* Port ids */
  152. #define CCI400_PORT_S0 0
  153. #define CCI400_PORT_S1 1
  154. #define CCI400_PORT_S2 2
  155. #define CCI400_PORT_S3 3
  156. #define CCI400_PORT_S4 4
  157. #define CCI400_PORT_M0 5
  158. #define CCI400_PORT_M1 6
  159. #define CCI400_PORT_M2 7
  160. #define CCI400_R1_PX 5
  161. /*
  162. * Instead of an event id to monitor CCI cycles, a dedicated counter is
  163. * provided. Use 0xff to represent CCI cycles and hope that no future revisions
  164. * make use of this event in hardware.
  165. */
  166. enum cci400_perf_events {
  167. CCI400_PMU_CYCLES = 0xff
  168. };
  169. #define CCI400_PMU_CYCLE_CNTR_IDX 0
  170. #define CCI400_PMU_CNTR0_IDX 1
  171. /*
  172. * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
  173. * ports and bits 4:0 are event codes. There are different event codes
  174. * associated with each port type.
  175. *
  176. * Additionally, the range of events associated with the port types changed
  177. * between Rev0 and Rev1.
  178. *
  179. * The constants below define the range of valid codes for each port type for
  180. * the different revisions and are used to validate the event to be monitored.
  181. */
  182. #define CCI400_PMU_EVENT_MASK 0xffUL
  183. #define CCI400_PMU_EVENT_SOURCE_SHIFT 5
  184. #define CCI400_PMU_EVENT_SOURCE_MASK 0x7
  185. #define CCI400_PMU_EVENT_CODE_SHIFT 0
  186. #define CCI400_PMU_EVENT_CODE_MASK 0x1f
  187. #define CCI400_PMU_EVENT_SOURCE(event) \
  188. ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
  189. CCI400_PMU_EVENT_SOURCE_MASK)
  190. #define CCI400_PMU_EVENT_CODE(event) \
  191. ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
  192. #define CCI400_R0_SLAVE_PORT_MIN_EV 0x00
  193. #define CCI400_R0_SLAVE_PORT_MAX_EV 0x13
  194. #define CCI400_R0_MASTER_PORT_MIN_EV 0x14
  195. #define CCI400_R0_MASTER_PORT_MAX_EV 0x1a
  196. #define CCI400_R1_SLAVE_PORT_MIN_EV 0x00
  197. #define CCI400_R1_SLAVE_PORT_MAX_EV 0x14
  198. #define CCI400_R1_MASTER_PORT_MIN_EV 0x00
  199. #define CCI400_R1_MASTER_PORT_MAX_EV 0x11
  200. #define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
  201. CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
  202. (unsigned long)_config)
  203. static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
  204. struct device_attribute *attr, char *buf);
  205. static struct dev_ext_attribute cci400_pmu_format_attrs[] = {
  206. CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
  207. CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-7"),
  208. };
  209. static struct dev_ext_attribute cci400_r0_pmu_event_attrs[] = {
  210. /* Slave events */
  211. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
  212. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
  213. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
  214. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
  215. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
  216. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
  217. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
  218. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
  219. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
  220. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
  221. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
  222. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
  223. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
  224. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
  225. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
  226. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
  227. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
  228. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
  229. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
  230. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
  231. /* Master events */
  232. CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x14),
  233. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard, 0x15),
  234. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard, 0x16),
  235. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full, 0x17),
  236. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x18),
  237. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x19),
  238. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full, 0x1A),
  239. /* Special event for cycles counter */
  240. CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
  241. };
  242. static struct dev_ext_attribute cci400_r1_pmu_event_attrs[] = {
  243. /* Slave events */
  244. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
  245. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
  246. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
  247. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
  248. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
  249. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
  250. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
  251. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
  252. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
  253. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
  254. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
  255. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
  256. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
  257. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
  258. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
  259. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
  260. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
  261. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
  262. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
  263. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
  264. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard, 0x14),
  265. /* Master events */
  266. CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x0),
  267. CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard, 0x1),
  268. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard, 0x2),
  269. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full, 0x3),
  270. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x4),
  271. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x5),
  272. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full, 0x6),
  273. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full, 0x7),
  274. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full, 0x8),
  275. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0, 0x9),
  276. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1, 0xA),
  277. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2, 0xB),
  278. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3, 0xC),
  279. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0, 0xD),
  280. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1, 0xE),
  281. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2, 0xF),
  282. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3, 0x10),
  283. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard, 0x11),
  284. /* Special event for cycles counter */
  285. CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
  286. };
  287. static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
  288. struct device_attribute *attr, char *buf)
  289. {
  290. struct dev_ext_attribute *eattr = container_of(attr,
  291. struct dev_ext_attribute, attr);
  292. return snprintf(buf, PAGE_SIZE, "config=0x%lx\n", (unsigned long)eattr->var);
  293. }
  294. static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
  295. struct cci_pmu_hw_events *hw,
  296. unsigned long cci_event)
  297. {
  298. int idx;
  299. /* cycles event idx is fixed */
  300. if (cci_event == CCI400_PMU_CYCLES) {
  301. if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
  302. return -EAGAIN;
  303. return CCI400_PMU_CYCLE_CNTR_IDX;
  304. }
  305. for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
  306. if (!test_and_set_bit(idx, hw->used_mask))
  307. return idx;
  308. /* No counters available */
  309. return -EAGAIN;
  310. }
  311. static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
  312. {
  313. u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
  314. u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
  315. int if_type;
  316. if (hw_event & ~CCI400_PMU_EVENT_MASK)
  317. return -ENOENT;
  318. if (hw_event == CCI400_PMU_CYCLES)
  319. return hw_event;
  320. switch (ev_source) {
  321. case CCI400_PORT_S0:
  322. case CCI400_PORT_S1:
  323. case CCI400_PORT_S2:
  324. case CCI400_PORT_S3:
  325. case CCI400_PORT_S4:
  326. /* Slave Interface */
  327. if_type = CCI_IF_SLAVE;
  328. break;
  329. case CCI400_PORT_M0:
  330. case CCI400_PORT_M1:
  331. case CCI400_PORT_M2:
  332. /* Master Interface */
  333. if_type = CCI_IF_MASTER;
  334. break;
  335. default:
  336. return -ENOENT;
  337. }
  338. if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
  339. ev_code <= cci_pmu->model->event_ranges[if_type].max)
  340. return hw_event;
  341. return -ENOENT;
  342. }
  343. static int probe_cci400_revision(void)
  344. {
  345. int rev;
  346. rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
  347. rev >>= CCI_PID2_REV_SHIFT;
  348. if (rev < CCI400_R1_PX)
  349. return CCI400_R0;
  350. else
  351. return CCI400_R1;
  352. }
  353. static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
  354. {
  355. if (platform_has_secure_cci_access())
  356. return &cci_pmu_models[probe_cci400_revision()];
  357. return NULL;
  358. }
  359. #else /* !CONFIG_ARM_CCI400_PMU */
  360. static inline struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
  361. {
  362. return NULL;
  363. }
  364. #endif /* CONFIG_ARM_CCI400_PMU */
  365. #ifdef CONFIG_ARM_CCI500_PMU
  366. /*
  367. * CCI500 provides 8 independent event counters that can count
  368. * any of the events available.
  369. *
  370. * CCI500 PMU event id is an 9-bit value made of two parts.
  371. * bits [8:5] - Source for the event
  372. * 0x0-0x6 - Slave interfaces
  373. * 0x8-0xD - Master interfaces
  374. * 0xf - Global Events
  375. * 0x7,0xe - Reserved
  376. *
  377. * bits [4:0] - Event code (specific to type of interface)
  378. */
  379. /* Port ids */
  380. #define CCI500_PORT_S0 0x0
  381. #define CCI500_PORT_S1 0x1
  382. #define CCI500_PORT_S2 0x2
  383. #define CCI500_PORT_S3 0x3
  384. #define CCI500_PORT_S4 0x4
  385. #define CCI500_PORT_S5 0x5
  386. #define CCI500_PORT_S6 0x6
  387. #define CCI500_PORT_M0 0x8
  388. #define CCI500_PORT_M1 0x9
  389. #define CCI500_PORT_M2 0xa
  390. #define CCI500_PORT_M3 0xb
  391. #define CCI500_PORT_M4 0xc
  392. #define CCI500_PORT_M5 0xd
  393. #define CCI500_PORT_GLOBAL 0xf
  394. #define CCI500_PMU_EVENT_MASK 0x1ffUL
  395. #define CCI500_PMU_EVENT_SOURCE_SHIFT 0x5
  396. #define CCI500_PMU_EVENT_SOURCE_MASK 0xf
  397. #define CCI500_PMU_EVENT_CODE_SHIFT 0x0
  398. #define CCI500_PMU_EVENT_CODE_MASK 0x1f
  399. #define CCI500_PMU_EVENT_SOURCE(event) \
  400. ((event >> CCI500_PMU_EVENT_SOURCE_SHIFT) & CCI500_PMU_EVENT_SOURCE_MASK)
  401. #define CCI500_PMU_EVENT_CODE(event) \
  402. ((event >> CCI500_PMU_EVENT_CODE_SHIFT) & CCI500_PMU_EVENT_CODE_MASK)
  403. #define CCI500_SLAVE_PORT_MIN_EV 0x00
  404. #define CCI500_SLAVE_PORT_MAX_EV 0x1f
  405. #define CCI500_MASTER_PORT_MIN_EV 0x00
  406. #define CCI500_MASTER_PORT_MAX_EV 0x06
  407. #define CCI500_GLOBAL_PORT_MIN_EV 0x00
  408. #define CCI500_GLOBAL_PORT_MAX_EV 0x0f
  409. #define CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
  410. CCI_EXT_ATTR_ENTRY(_name, cci500_pmu_global_event_show, \
  411. (unsigned long) _config)
  412. static ssize_t cci500_pmu_global_event_show(struct device *dev,
  413. struct device_attribute *attr, char *buf);
  414. static struct dev_ext_attribute cci500_pmu_format_attrs[] = {
  415. CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
  416. CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-8"),
  417. };
  418. static struct dev_ext_attribute cci500_pmu_event_attrs[] = {
  419. /* Slave events */
  420. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_arvalid, 0x0),
  421. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_dev, 0x1),
  422. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_nonshareable, 0x2),
  423. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_non_alloc, 0x3),
  424. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_alloc, 0x4),
  425. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_invalidate, 0x5),
  426. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maint, 0x6),
  427. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
  428. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rval, 0x8),
  429. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rlast_snoop, 0x9),
  430. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_awalid, 0xA),
  431. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_dev, 0xB),
  432. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_non_shareable, 0xC),
  433. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wb, 0xD),
  434. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wlu, 0xE),
  435. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wunique, 0xF),
  436. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_evict, 0x10),
  437. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_wrevict, 0x11),
  438. CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_beat, 0x12),
  439. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_acvalid, 0x13),
  440. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_read, 0x14),
  441. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_clean, 0x15),
  442. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_data_transfer_low, 0x16),
  443. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_arvalid, 0x17),
  444. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall, 0x18),
  445. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall, 0x19),
  446. CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_stall, 0x1A),
  447. CCI_EVENT_EXT_ATTR_ENTRY(si_w_resp_stall, 0x1B),
  448. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_stall, 0x1C),
  449. CCI_EVENT_EXT_ATTR_ENTRY(si_s_data_stall, 0x1D),
  450. CCI_EVENT_EXT_ATTR_ENTRY(si_rq_stall_ot_limit, 0x1E),
  451. CCI_EVENT_EXT_ATTR_ENTRY(si_r_stall_arbit, 0x1F),
  452. /* Master events */
  453. CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_beat_any, 0x0),
  454. CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_beat_any, 0x1),
  455. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall, 0x2),
  456. CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_stall, 0x3),
  457. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall, 0x4),
  458. CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_stall, 0x5),
  459. CCI_EVENT_EXT_ATTR_ENTRY(mi_w_resp_stall, 0x6),
  460. /* Global events */
  461. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1, 0x0),
  462. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3, 0x1),
  463. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5, 0x2),
  464. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7, 0x3),
  465. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1, 0x4),
  466. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3, 0x5),
  467. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5, 0x6),
  468. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7, 0x7),
  469. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation, 0x8),
  470. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy, 0x9),
  471. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full, 0xA),
  472. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq, 0xB),
  473. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs, 0xC),
  474. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard, 0xD),
  475. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snopp_rq_stall_tt_full, 0xE),
  476. CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot, 0xF),
  477. };
  478. static ssize_t cci500_pmu_global_event_show(struct device *dev,
  479. struct device_attribute *attr, char *buf)
  480. {
  481. struct dev_ext_attribute *eattr = container_of(attr,
  482. struct dev_ext_attribute, attr);
  483. /* Global events have single fixed source code */
  484. return snprintf(buf, PAGE_SIZE, "event=0x%lx,source=0x%x\n",
  485. (unsigned long)eattr->var, CCI500_PORT_GLOBAL);
  486. }
  487. static int cci500_validate_hw_event(struct cci_pmu *cci_pmu,
  488. unsigned long hw_event)
  489. {
  490. u32 ev_source = CCI500_PMU_EVENT_SOURCE(hw_event);
  491. u32 ev_code = CCI500_PMU_EVENT_CODE(hw_event);
  492. int if_type;
  493. if (hw_event & ~CCI500_PMU_EVENT_MASK)
  494. return -ENOENT;
  495. switch (ev_source) {
  496. case CCI500_PORT_S0:
  497. case CCI500_PORT_S1:
  498. case CCI500_PORT_S2:
  499. case CCI500_PORT_S3:
  500. case CCI500_PORT_S4:
  501. case CCI500_PORT_S5:
  502. case CCI500_PORT_S6:
  503. if_type = CCI_IF_SLAVE;
  504. break;
  505. case CCI500_PORT_M0:
  506. case CCI500_PORT_M1:
  507. case CCI500_PORT_M2:
  508. case CCI500_PORT_M3:
  509. case CCI500_PORT_M4:
  510. case CCI500_PORT_M5:
  511. if_type = CCI_IF_MASTER;
  512. break;
  513. case CCI500_PORT_GLOBAL:
  514. if_type = CCI_IF_GLOBAL;
  515. break;
  516. default:
  517. return -ENOENT;
  518. }
  519. if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
  520. ev_code <= cci_pmu->model->event_ranges[if_type].max)
  521. return hw_event;
  522. return -ENOENT;
  523. }
  524. #endif /* CONFIG_ARM_CCI500_PMU */
  525. static ssize_t cci_pmu_format_show(struct device *dev,
  526. struct device_attribute *attr, char *buf)
  527. {
  528. struct dev_ext_attribute *eattr = container_of(attr,
  529. struct dev_ext_attribute, attr);
  530. return snprintf(buf, PAGE_SIZE, "%s\n", (char *)eattr->var);
  531. }
  532. static ssize_t cci_pmu_event_show(struct device *dev,
  533. struct device_attribute *attr, char *buf)
  534. {
  535. struct dev_ext_attribute *eattr = container_of(attr,
  536. struct dev_ext_attribute, attr);
  537. /* source parameter is mandatory for normal PMU events */
  538. return snprintf(buf, PAGE_SIZE, "source=?,event=0x%lx\n",
  539. (unsigned long)eattr->var);
  540. }
  541. static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
  542. {
  543. return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
  544. }
  545. static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
  546. {
  547. return readl_relaxed(cci_pmu->base +
  548. CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
  549. }
  550. static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
  551. int idx, unsigned int offset)
  552. {
  553. return writel_relaxed(value, cci_pmu->base +
  554. CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
  555. }
  556. static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
  557. {
  558. pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
  559. }
  560. static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
  561. {
  562. pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
  563. }
  564. static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
  565. {
  566. pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
  567. }
  568. /*
  569. * Returns the number of programmable counters actually implemented
  570. * by the cci
  571. */
  572. static u32 pmu_get_max_counters(void)
  573. {
  574. return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
  575. CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
  576. }
  577. static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
  578. {
  579. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  580. unsigned long cci_event = event->hw.config_base;
  581. int idx;
  582. if (cci_pmu->model->get_event_idx)
  583. return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
  584. /* Generic code to find an unused idx from the mask */
  585. for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
  586. if (!test_and_set_bit(idx, hw->used_mask))
  587. return idx;
  588. /* No counters available */
  589. return -EAGAIN;
  590. }
  591. static int pmu_map_event(struct perf_event *event)
  592. {
  593. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  594. if (event->attr.type < PERF_TYPE_MAX ||
  595. !cci_pmu->model->validate_hw_event)
  596. return -ENOENT;
  597. return cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
  598. }
  599. static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
  600. {
  601. int i;
  602. struct platform_device *pmu_device = cci_pmu->plat_device;
  603. if (unlikely(!pmu_device))
  604. return -ENODEV;
  605. if (cci_pmu->nr_irqs < 1) {
  606. dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
  607. return -ENODEV;
  608. }
  609. /*
  610. * Register all available CCI PMU interrupts. In the interrupt handler
  611. * we iterate over the counters checking for interrupt source (the
  612. * overflowing counter) and clear it.
  613. *
  614. * This should allow handling of non-unique interrupt for the counters.
  615. */
  616. for (i = 0; i < cci_pmu->nr_irqs; i++) {
  617. int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
  618. "arm-cci-pmu", cci_pmu);
  619. if (err) {
  620. dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
  621. cci_pmu->irqs[i]);
  622. return err;
  623. }
  624. set_bit(i, &cci_pmu->active_irqs);
  625. }
  626. return 0;
  627. }
  628. static void pmu_free_irq(struct cci_pmu *cci_pmu)
  629. {
  630. int i;
  631. for (i = 0; i < cci_pmu->nr_irqs; i++) {
  632. if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
  633. continue;
  634. free_irq(cci_pmu->irqs[i], cci_pmu);
  635. }
  636. }
  637. static u32 pmu_read_counter(struct perf_event *event)
  638. {
  639. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  640. struct hw_perf_event *hw_counter = &event->hw;
  641. int idx = hw_counter->idx;
  642. u32 value;
  643. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  644. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  645. return 0;
  646. }
  647. value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
  648. return value;
  649. }
  650. static void pmu_write_counter(struct perf_event *event, u32 value)
  651. {
  652. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  653. struct hw_perf_event *hw_counter = &event->hw;
  654. int idx = hw_counter->idx;
  655. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
  656. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  657. else
  658. pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
  659. }
  660. static u64 pmu_event_update(struct perf_event *event)
  661. {
  662. struct hw_perf_event *hwc = &event->hw;
  663. u64 delta, prev_raw_count, new_raw_count;
  664. do {
  665. prev_raw_count = local64_read(&hwc->prev_count);
  666. new_raw_count = pmu_read_counter(event);
  667. } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  668. new_raw_count) != prev_raw_count);
  669. delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
  670. local64_add(delta, &event->count);
  671. return new_raw_count;
  672. }
  673. static void pmu_read(struct perf_event *event)
  674. {
  675. pmu_event_update(event);
  676. }
  677. void pmu_event_set_period(struct perf_event *event)
  678. {
  679. struct hw_perf_event *hwc = &event->hw;
  680. /*
  681. * The CCI PMU counters have a period of 2^32. To account for the
  682. * possiblity of extreme interrupt latency we program for a period of
  683. * half that. Hopefully we can handle the interrupt before another 2^31
  684. * events occur and the counter overtakes its previous value.
  685. */
  686. u64 val = 1ULL << 31;
  687. local64_set(&hwc->prev_count, val);
  688. pmu_write_counter(event, val);
  689. }
  690. static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
  691. {
  692. unsigned long flags;
  693. struct cci_pmu *cci_pmu = dev;
  694. struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
  695. int idx, handled = IRQ_NONE;
  696. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  697. /*
  698. * Iterate over counters and update the corresponding perf events.
  699. * This should work regardless of whether we have per-counter overflow
  700. * interrupt or a combined overflow interrupt.
  701. */
  702. for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
  703. struct perf_event *event = events->events[idx];
  704. struct hw_perf_event *hw_counter;
  705. if (!event)
  706. continue;
  707. hw_counter = &event->hw;
  708. /* Did this counter overflow? */
  709. if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
  710. CCI_PMU_OVRFLW_FLAG))
  711. continue;
  712. pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
  713. CCI_PMU_OVRFLW);
  714. pmu_event_update(event);
  715. pmu_event_set_period(event);
  716. handled = IRQ_HANDLED;
  717. }
  718. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  719. return IRQ_RETVAL(handled);
  720. }
  721. static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
  722. {
  723. int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
  724. if (ret) {
  725. pmu_free_irq(cci_pmu);
  726. return ret;
  727. }
  728. return 0;
  729. }
  730. static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
  731. {
  732. pmu_free_irq(cci_pmu);
  733. }
  734. static void hw_perf_event_destroy(struct perf_event *event)
  735. {
  736. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  737. atomic_t *active_events = &cci_pmu->active_events;
  738. struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
  739. if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
  740. cci_pmu_put_hw(cci_pmu);
  741. mutex_unlock(reserve_mutex);
  742. }
  743. }
  744. static void cci_pmu_enable(struct pmu *pmu)
  745. {
  746. struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
  747. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  748. int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
  749. unsigned long flags;
  750. u32 val;
  751. if (!enabled)
  752. return;
  753. raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
  754. /* Enable all the PMU counters. */
  755. val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
  756. writel(val, cci_ctrl_base + CCI_PMCR);
  757. raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
  758. }
  759. static void cci_pmu_disable(struct pmu *pmu)
  760. {
  761. struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
  762. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  763. unsigned long flags;
  764. u32 val;
  765. raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
  766. /* Disable all the PMU counters. */
  767. val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
  768. writel(val, cci_ctrl_base + CCI_PMCR);
  769. raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
  770. }
  771. /*
  772. * Check if the idx represents a non-programmable counter.
  773. * All the fixed event counters are mapped before the programmable
  774. * counters.
  775. */
  776. static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
  777. {
  778. return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
  779. }
  780. static void cci_pmu_start(struct perf_event *event, int pmu_flags)
  781. {
  782. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  783. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  784. struct hw_perf_event *hwc = &event->hw;
  785. int idx = hwc->idx;
  786. unsigned long flags;
  787. /*
  788. * To handle interrupt latency, we always reprogram the period
  789. * regardlesss of PERF_EF_RELOAD.
  790. */
  791. if (pmu_flags & PERF_EF_RELOAD)
  792. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  793. hwc->state = 0;
  794. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  795. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  796. return;
  797. }
  798. raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
  799. /* Configure the counter unless you are counting a fixed event */
  800. if (!pmu_fixed_hw_idx(cci_pmu, idx))
  801. pmu_set_event(cci_pmu, idx, hwc->config_base);
  802. pmu_event_set_period(event);
  803. pmu_enable_counter(cci_pmu, idx);
  804. raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
  805. }
  806. static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
  807. {
  808. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  809. struct hw_perf_event *hwc = &event->hw;
  810. int idx = hwc->idx;
  811. if (hwc->state & PERF_HES_STOPPED)
  812. return;
  813. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  814. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  815. return;
  816. }
  817. /*
  818. * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
  819. * cci_pmu_start()
  820. */
  821. pmu_disable_counter(cci_pmu, idx);
  822. pmu_event_update(event);
  823. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  824. }
  825. static int cci_pmu_add(struct perf_event *event, int flags)
  826. {
  827. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  828. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  829. struct hw_perf_event *hwc = &event->hw;
  830. int idx;
  831. int err = 0;
  832. perf_pmu_disable(event->pmu);
  833. /* If we don't have a space for the counter then finish early. */
  834. idx = pmu_get_event_idx(hw_events, event);
  835. if (idx < 0) {
  836. err = idx;
  837. goto out;
  838. }
  839. event->hw.idx = idx;
  840. hw_events->events[idx] = event;
  841. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  842. if (flags & PERF_EF_START)
  843. cci_pmu_start(event, PERF_EF_RELOAD);
  844. /* Propagate our changes to the userspace mapping. */
  845. perf_event_update_userpage(event);
  846. out:
  847. perf_pmu_enable(event->pmu);
  848. return err;
  849. }
  850. static void cci_pmu_del(struct perf_event *event, int flags)
  851. {
  852. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  853. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  854. struct hw_perf_event *hwc = &event->hw;
  855. int idx = hwc->idx;
  856. cci_pmu_stop(event, PERF_EF_UPDATE);
  857. hw_events->events[idx] = NULL;
  858. clear_bit(idx, hw_events->used_mask);
  859. perf_event_update_userpage(event);
  860. }
  861. static int
  862. validate_event(struct pmu *cci_pmu,
  863. struct cci_pmu_hw_events *hw_events,
  864. struct perf_event *event)
  865. {
  866. if (is_software_event(event))
  867. return 1;
  868. /*
  869. * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
  870. * core perf code won't check that the pmu->ctx == leader->ctx
  871. * until after pmu->event_init(event).
  872. */
  873. if (event->pmu != cci_pmu)
  874. return 0;
  875. if (event->state < PERF_EVENT_STATE_OFF)
  876. return 1;
  877. if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
  878. return 1;
  879. return pmu_get_event_idx(hw_events, event) >= 0;
  880. }
  881. static int
  882. validate_group(struct perf_event *event)
  883. {
  884. struct perf_event *sibling, *leader = event->group_leader;
  885. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  886. unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
  887. struct cci_pmu_hw_events fake_pmu = {
  888. /*
  889. * Initialise the fake PMU. We only need to populate the
  890. * used_mask for the purposes of validation.
  891. */
  892. .used_mask = mask,
  893. };
  894. memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
  895. if (!validate_event(event->pmu, &fake_pmu, leader))
  896. return -EINVAL;
  897. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  898. if (!validate_event(event->pmu, &fake_pmu, sibling))
  899. return -EINVAL;
  900. }
  901. if (!validate_event(event->pmu, &fake_pmu, event))
  902. return -EINVAL;
  903. return 0;
  904. }
  905. static int
  906. __hw_perf_event_init(struct perf_event *event)
  907. {
  908. struct hw_perf_event *hwc = &event->hw;
  909. int mapping;
  910. mapping = pmu_map_event(event);
  911. if (mapping < 0) {
  912. pr_debug("event %x:%llx not supported\n", event->attr.type,
  913. event->attr.config);
  914. return mapping;
  915. }
  916. /*
  917. * We don't assign an index until we actually place the event onto
  918. * hardware. Use -1 to signify that we haven't decided where to put it
  919. * yet.
  920. */
  921. hwc->idx = -1;
  922. hwc->config_base = 0;
  923. hwc->config = 0;
  924. hwc->event_base = 0;
  925. /*
  926. * Store the event encoding into the config_base field.
  927. */
  928. hwc->config_base |= (unsigned long)mapping;
  929. /*
  930. * Limit the sample_period to half of the counter width. That way, the
  931. * new counter value is far less likely to overtake the previous one
  932. * unless you have some serious IRQ latency issues.
  933. */
  934. hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
  935. hwc->last_period = hwc->sample_period;
  936. local64_set(&hwc->period_left, hwc->sample_period);
  937. if (event->group_leader != event) {
  938. if (validate_group(event) != 0)
  939. return -EINVAL;
  940. }
  941. return 0;
  942. }
  943. static int cci_pmu_event_init(struct perf_event *event)
  944. {
  945. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  946. atomic_t *active_events = &cci_pmu->active_events;
  947. int err = 0;
  948. int cpu;
  949. if (event->attr.type != event->pmu->type)
  950. return -ENOENT;
  951. /* Shared by all CPUs, no meaningful state to sample */
  952. if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
  953. return -EOPNOTSUPP;
  954. /* We have no filtering of any kind */
  955. if (event->attr.exclude_user ||
  956. event->attr.exclude_kernel ||
  957. event->attr.exclude_hv ||
  958. event->attr.exclude_idle ||
  959. event->attr.exclude_host ||
  960. event->attr.exclude_guest)
  961. return -EINVAL;
  962. /*
  963. * Following the example set by other "uncore" PMUs, we accept any CPU
  964. * and rewrite its affinity dynamically rather than having perf core
  965. * handle cpu == -1 and pid == -1 for this case.
  966. *
  967. * The perf core will pin online CPUs for the duration of this call and
  968. * the event being installed into its context, so the PMU's CPU can't
  969. * change under our feet.
  970. */
  971. cpu = cpumask_first(&cci_pmu->cpus);
  972. if (event->cpu < 0 || cpu < 0)
  973. return -EINVAL;
  974. event->cpu = cpu;
  975. event->destroy = hw_perf_event_destroy;
  976. if (!atomic_inc_not_zero(active_events)) {
  977. mutex_lock(&cci_pmu->reserve_mutex);
  978. if (atomic_read(active_events) == 0)
  979. err = cci_pmu_get_hw(cci_pmu);
  980. if (!err)
  981. atomic_inc(active_events);
  982. mutex_unlock(&cci_pmu->reserve_mutex);
  983. }
  984. if (err)
  985. return err;
  986. err = __hw_perf_event_init(event);
  987. if (err)
  988. hw_perf_event_destroy(event);
  989. return err;
  990. }
  991. static ssize_t pmu_cpumask_attr_show(struct device *dev,
  992. struct device_attribute *attr, char *buf)
  993. {
  994. struct dev_ext_attribute *eattr = container_of(attr,
  995. struct dev_ext_attribute, attr);
  996. struct cci_pmu *cci_pmu = eattr->var;
  997. int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  998. cpumask_pr_args(&cci_pmu->cpus));
  999. buf[n++] = '\n';
  1000. buf[n] = '\0';
  1001. return n;
  1002. }
  1003. static struct dev_ext_attribute pmu_cpumask_attr = {
  1004. __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL),
  1005. NULL, /* Populated in cci_pmu_init */
  1006. };
  1007. static struct attribute *pmu_attrs[] = {
  1008. &pmu_cpumask_attr.attr.attr,
  1009. NULL,
  1010. };
  1011. static struct attribute_group pmu_attr_group = {
  1012. .attrs = pmu_attrs,
  1013. };
  1014. static struct attribute_group pmu_format_attr_group = {
  1015. .name = "format",
  1016. .attrs = NULL, /* Filled in cci_pmu_init_attrs */
  1017. };
  1018. static struct attribute_group pmu_event_attr_group = {
  1019. .name = "events",
  1020. .attrs = NULL, /* Filled in cci_pmu_init_attrs */
  1021. };
  1022. static const struct attribute_group *pmu_attr_groups[] = {
  1023. &pmu_attr_group,
  1024. &pmu_format_attr_group,
  1025. &pmu_event_attr_group,
  1026. NULL
  1027. };
  1028. static struct attribute **alloc_attrs(struct platform_device *pdev,
  1029. int n, struct dev_ext_attribute *source)
  1030. {
  1031. int i;
  1032. struct attribute **attrs;
  1033. /* Alloc n + 1 (for terminating NULL) */
  1034. attrs = devm_kcalloc(&pdev->dev, n + 1, sizeof(struct attribute *),
  1035. GFP_KERNEL);
  1036. if (!attrs)
  1037. return attrs;
  1038. for(i = 0; i < n; i++)
  1039. attrs[i] = &source[i].attr.attr;
  1040. return attrs;
  1041. }
  1042. static int cci_pmu_init_attrs(struct cci_pmu *cci_pmu, struct platform_device *pdev)
  1043. {
  1044. const struct cci_pmu_model *model = cci_pmu->model;
  1045. struct attribute **attrs;
  1046. /*
  1047. * All allocations below are managed, hence doesn't need to be
  1048. * free'd explicitly in case of an error.
  1049. */
  1050. if (model->nevent_attrs) {
  1051. attrs = alloc_attrs(pdev, model->nevent_attrs,
  1052. model->event_attrs);
  1053. if (!attrs)
  1054. return -ENOMEM;
  1055. pmu_event_attr_group.attrs = attrs;
  1056. }
  1057. if (model->nformat_attrs) {
  1058. attrs = alloc_attrs(pdev, model->nformat_attrs,
  1059. model->format_attrs);
  1060. if (!attrs)
  1061. return -ENOMEM;
  1062. pmu_format_attr_group.attrs = attrs;
  1063. }
  1064. pmu_cpumask_attr.var = cci_pmu;
  1065. return 0;
  1066. }
  1067. static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
  1068. {
  1069. char *name = cci_pmu->model->name;
  1070. u32 num_cntrs;
  1071. int rc;
  1072. rc = cci_pmu_init_attrs(cci_pmu, pdev);
  1073. if (rc)
  1074. return rc;
  1075. cci_pmu->pmu = (struct pmu) {
  1076. .name = cci_pmu->model->name,
  1077. .task_ctx_nr = perf_invalid_context,
  1078. .pmu_enable = cci_pmu_enable,
  1079. .pmu_disable = cci_pmu_disable,
  1080. .event_init = cci_pmu_event_init,
  1081. .add = cci_pmu_add,
  1082. .del = cci_pmu_del,
  1083. .start = cci_pmu_start,
  1084. .stop = cci_pmu_stop,
  1085. .read = pmu_read,
  1086. .attr_groups = pmu_attr_groups,
  1087. };
  1088. cci_pmu->plat_device = pdev;
  1089. num_cntrs = pmu_get_max_counters();
  1090. if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
  1091. dev_warn(&pdev->dev,
  1092. "PMU implements more counters(%d) than supported by"
  1093. " the model(%d), truncated.",
  1094. num_cntrs, cci_pmu->model->num_hw_cntrs);
  1095. num_cntrs = cci_pmu->model->num_hw_cntrs;
  1096. }
  1097. cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
  1098. return perf_pmu_register(&cci_pmu->pmu, name, -1);
  1099. }
  1100. static int cci_pmu_cpu_notifier(struct notifier_block *self,
  1101. unsigned long action, void *hcpu)
  1102. {
  1103. struct cci_pmu *cci_pmu = container_of(self,
  1104. struct cci_pmu, cpu_nb);
  1105. unsigned int cpu = (long)hcpu;
  1106. unsigned int target;
  1107. switch (action & ~CPU_TASKS_FROZEN) {
  1108. case CPU_DOWN_PREPARE:
  1109. if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
  1110. break;
  1111. target = cpumask_any_but(cpu_online_mask, cpu);
  1112. if (target < 0) // UP, last CPU
  1113. break;
  1114. /*
  1115. * TODO: migrate context once core races on event->ctx have
  1116. * been fixed.
  1117. */
  1118. cpumask_set_cpu(target, &cci_pmu->cpus);
  1119. default:
  1120. break;
  1121. }
  1122. return NOTIFY_OK;
  1123. }
  1124. static struct cci_pmu_model cci_pmu_models[] = {
  1125. #ifdef CONFIG_ARM_CCI400_PMU
  1126. [CCI400_R0] = {
  1127. .name = "CCI_400",
  1128. .fixed_hw_cntrs = 1, /* Cycle counter */
  1129. .num_hw_cntrs = 4,
  1130. .cntr_size = SZ_4K,
  1131. .format_attrs = cci400_pmu_format_attrs,
  1132. .nformat_attrs = ARRAY_SIZE(cci400_pmu_format_attrs),
  1133. .event_attrs = cci400_r0_pmu_event_attrs,
  1134. .nevent_attrs = ARRAY_SIZE(cci400_r0_pmu_event_attrs),
  1135. .event_ranges = {
  1136. [CCI_IF_SLAVE] = {
  1137. CCI400_R0_SLAVE_PORT_MIN_EV,
  1138. CCI400_R0_SLAVE_PORT_MAX_EV,
  1139. },
  1140. [CCI_IF_MASTER] = {
  1141. CCI400_R0_MASTER_PORT_MIN_EV,
  1142. CCI400_R0_MASTER_PORT_MAX_EV,
  1143. },
  1144. },
  1145. .validate_hw_event = cci400_validate_hw_event,
  1146. .get_event_idx = cci400_get_event_idx,
  1147. },
  1148. [CCI400_R1] = {
  1149. .name = "CCI_400_r1",
  1150. .fixed_hw_cntrs = 1, /* Cycle counter */
  1151. .num_hw_cntrs = 4,
  1152. .cntr_size = SZ_4K,
  1153. .format_attrs = cci400_pmu_format_attrs,
  1154. .nformat_attrs = ARRAY_SIZE(cci400_pmu_format_attrs),
  1155. .event_attrs = cci400_r1_pmu_event_attrs,
  1156. .nevent_attrs = ARRAY_SIZE(cci400_r1_pmu_event_attrs),
  1157. .event_ranges = {
  1158. [CCI_IF_SLAVE] = {
  1159. CCI400_R1_SLAVE_PORT_MIN_EV,
  1160. CCI400_R1_SLAVE_PORT_MAX_EV,
  1161. },
  1162. [CCI_IF_MASTER] = {
  1163. CCI400_R1_MASTER_PORT_MIN_EV,
  1164. CCI400_R1_MASTER_PORT_MAX_EV,
  1165. },
  1166. },
  1167. .validate_hw_event = cci400_validate_hw_event,
  1168. .get_event_idx = cci400_get_event_idx,
  1169. },
  1170. #endif
  1171. #ifdef CONFIG_ARM_CCI500_PMU
  1172. [CCI500_R0] = {
  1173. .name = "CCI_500",
  1174. .fixed_hw_cntrs = 0,
  1175. .num_hw_cntrs = 8,
  1176. .cntr_size = SZ_64K,
  1177. .format_attrs = cci500_pmu_format_attrs,
  1178. .nformat_attrs = ARRAY_SIZE(cci500_pmu_format_attrs),
  1179. .event_attrs = cci500_pmu_event_attrs,
  1180. .nevent_attrs = ARRAY_SIZE(cci500_pmu_event_attrs),
  1181. .event_ranges = {
  1182. [CCI_IF_SLAVE] = {
  1183. CCI500_SLAVE_PORT_MIN_EV,
  1184. CCI500_SLAVE_PORT_MAX_EV,
  1185. },
  1186. [CCI_IF_MASTER] = {
  1187. CCI500_MASTER_PORT_MIN_EV,
  1188. CCI500_MASTER_PORT_MAX_EV,
  1189. },
  1190. [CCI_IF_GLOBAL] = {
  1191. CCI500_GLOBAL_PORT_MIN_EV,
  1192. CCI500_GLOBAL_PORT_MAX_EV,
  1193. },
  1194. },
  1195. .validate_hw_event = cci500_validate_hw_event,
  1196. },
  1197. #endif
  1198. };
  1199. static const struct of_device_id arm_cci_pmu_matches[] = {
  1200. #ifdef CONFIG_ARM_CCI400_PMU
  1201. {
  1202. .compatible = "arm,cci-400-pmu",
  1203. .data = NULL,
  1204. },
  1205. {
  1206. .compatible = "arm,cci-400-pmu,r0",
  1207. .data = &cci_pmu_models[CCI400_R0],
  1208. },
  1209. {
  1210. .compatible = "arm,cci-400-pmu,r1",
  1211. .data = &cci_pmu_models[CCI400_R1],
  1212. },
  1213. #endif
  1214. #ifdef CONFIG_ARM_CCI500_PMU
  1215. {
  1216. .compatible = "arm,cci-500-pmu,r0",
  1217. .data = &cci_pmu_models[CCI500_R0],
  1218. },
  1219. #endif
  1220. {},
  1221. };
  1222. static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
  1223. {
  1224. const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
  1225. pdev->dev.of_node);
  1226. if (!match)
  1227. return NULL;
  1228. if (match->data)
  1229. return match->data;
  1230. dev_warn(&pdev->dev, "DEPRECATED compatible property,"
  1231. "requires secure access to CCI registers");
  1232. return probe_cci_model(pdev);
  1233. }
  1234. static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
  1235. {
  1236. int i;
  1237. for (i = 0; i < nr_irqs; i++)
  1238. if (irq == irqs[i])
  1239. return true;
  1240. return false;
  1241. }
  1242. static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
  1243. {
  1244. struct cci_pmu *cci_pmu;
  1245. const struct cci_pmu_model *model;
  1246. /*
  1247. * All allocations are devm_* hence we don't have to free
  1248. * them explicitly on an error, as it would end up in driver
  1249. * detach.
  1250. */
  1251. model = get_cci_model(pdev);
  1252. if (!model) {
  1253. dev_warn(&pdev->dev, "CCI PMU version not supported\n");
  1254. return ERR_PTR(-ENODEV);
  1255. }
  1256. cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
  1257. if (!cci_pmu)
  1258. return ERR_PTR(-ENOMEM);
  1259. cci_pmu->model = model;
  1260. cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
  1261. sizeof(*cci_pmu->irqs), GFP_KERNEL);
  1262. if (!cci_pmu->irqs)
  1263. return ERR_PTR(-ENOMEM);
  1264. cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
  1265. CCI_PMU_MAX_HW_CNTRS(model),
  1266. sizeof(*cci_pmu->hw_events.events),
  1267. GFP_KERNEL);
  1268. if (!cci_pmu->hw_events.events)
  1269. return ERR_PTR(-ENOMEM);
  1270. cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
  1271. BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
  1272. sizeof(*cci_pmu->hw_events.used_mask),
  1273. GFP_KERNEL);
  1274. if (!cci_pmu->hw_events.used_mask)
  1275. return ERR_PTR(-ENOMEM);
  1276. return cci_pmu;
  1277. }
  1278. static int cci_pmu_probe(struct platform_device *pdev)
  1279. {
  1280. struct resource *res;
  1281. struct cci_pmu *cci_pmu;
  1282. int i, ret, irq;
  1283. cci_pmu = cci_pmu_alloc(pdev);
  1284. if (IS_ERR(cci_pmu))
  1285. return PTR_ERR(cci_pmu);
  1286. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1287. cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
  1288. if (IS_ERR(cci_pmu->base))
  1289. return -ENOMEM;
  1290. /*
  1291. * CCI PMU has one overflow interrupt per counter; but some may be tied
  1292. * together to a common interrupt.
  1293. */
  1294. cci_pmu->nr_irqs = 0;
  1295. for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
  1296. irq = platform_get_irq(pdev, i);
  1297. if (irq < 0)
  1298. break;
  1299. if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
  1300. continue;
  1301. cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
  1302. }
  1303. /*
  1304. * Ensure that the device tree has as many interrupts as the number
  1305. * of counters.
  1306. */
  1307. if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
  1308. dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
  1309. i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
  1310. return -EINVAL;
  1311. }
  1312. raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
  1313. mutex_init(&cci_pmu->reserve_mutex);
  1314. atomic_set(&cci_pmu->active_events, 0);
  1315. cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
  1316. cci_pmu->cpu_nb = (struct notifier_block) {
  1317. .notifier_call = cci_pmu_cpu_notifier,
  1318. /*
  1319. * to migrate uncore events, our notifier should be executed
  1320. * before perf core's notifier.
  1321. */
  1322. .priority = CPU_PRI_PERF + 1,
  1323. };
  1324. ret = register_cpu_notifier(&cci_pmu->cpu_nb);
  1325. if (ret)
  1326. return ret;
  1327. ret = cci_pmu_init(cci_pmu, pdev);
  1328. if (ret) {
  1329. unregister_cpu_notifier(&cci_pmu->cpu_nb);
  1330. return ret;
  1331. }
  1332. pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
  1333. return 0;
  1334. }
  1335. static int cci_platform_probe(struct platform_device *pdev)
  1336. {
  1337. if (!cci_probed())
  1338. return -ENODEV;
  1339. return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  1340. }
  1341. static struct platform_driver cci_pmu_driver = {
  1342. .driver = {
  1343. .name = DRIVER_NAME_PMU,
  1344. .of_match_table = arm_cci_pmu_matches,
  1345. },
  1346. .probe = cci_pmu_probe,
  1347. };
  1348. static struct platform_driver cci_platform_driver = {
  1349. .driver = {
  1350. .name = DRIVER_NAME,
  1351. .of_match_table = arm_cci_matches,
  1352. },
  1353. .probe = cci_platform_probe,
  1354. };
  1355. static int __init cci_platform_init(void)
  1356. {
  1357. int ret;
  1358. ret = platform_driver_register(&cci_pmu_driver);
  1359. if (ret)
  1360. return ret;
  1361. return platform_driver_register(&cci_platform_driver);
  1362. }
  1363. #else /* !CONFIG_ARM_CCI_PMU */
  1364. static int __init cci_platform_init(void)
  1365. {
  1366. return 0;
  1367. }
  1368. #endif /* CONFIG_ARM_CCI_PMU */
  1369. #ifdef CONFIG_ARM_CCI400_PORT_CTRL
  1370. #define CCI_PORT_CTRL 0x0
  1371. #define CCI_CTRL_STATUS 0xc
  1372. #define CCI_ENABLE_SNOOP_REQ 0x1
  1373. #define CCI_ENABLE_DVM_REQ 0x2
  1374. #define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
  1375. enum cci_ace_port_type {
  1376. ACE_INVALID_PORT = 0x0,
  1377. ACE_PORT,
  1378. ACE_LITE_PORT,
  1379. };
  1380. struct cci_ace_port {
  1381. void __iomem *base;
  1382. unsigned long phys;
  1383. enum cci_ace_port_type type;
  1384. struct device_node *dn;
  1385. };
  1386. static struct cci_ace_port *ports;
  1387. static unsigned int nb_cci_ports;
  1388. struct cpu_port {
  1389. u64 mpidr;
  1390. u32 port;
  1391. };
  1392. /*
  1393. * Use the port MSB as valid flag, shift can be made dynamic
  1394. * by computing number of bits required for port indexes.
  1395. * Code disabling CCI cpu ports runs with D-cache invalidated
  1396. * and SCTLR bit clear so data accesses must be kept to a minimum
  1397. * to improve performance; for now shift is left static to
  1398. * avoid one more data access while disabling the CCI port.
  1399. */
  1400. #define PORT_VALID_SHIFT 31
  1401. #define PORT_VALID (0x1 << PORT_VALID_SHIFT)
  1402. static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
  1403. {
  1404. port->port = PORT_VALID | index;
  1405. port->mpidr = mpidr;
  1406. }
  1407. static inline bool cpu_port_is_valid(struct cpu_port *port)
  1408. {
  1409. return !!(port->port & PORT_VALID);
  1410. }
  1411. static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
  1412. {
  1413. return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
  1414. }
  1415. static struct cpu_port cpu_port[NR_CPUS];
  1416. /**
  1417. * __cci_ace_get_port - Function to retrieve the port index connected to
  1418. * a cpu or device.
  1419. *
  1420. * @dn: device node of the device to look-up
  1421. * @type: port type
  1422. *
  1423. * Return value:
  1424. * - CCI port index if success
  1425. * - -ENODEV if failure
  1426. */
  1427. static int __cci_ace_get_port(struct device_node *dn, int type)
  1428. {
  1429. int i;
  1430. bool ace_match;
  1431. struct device_node *cci_portn;
  1432. cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
  1433. for (i = 0; i < nb_cci_ports; i++) {
  1434. ace_match = ports[i].type == type;
  1435. if (ace_match && cci_portn == ports[i].dn)
  1436. return i;
  1437. }
  1438. return -ENODEV;
  1439. }
  1440. int cci_ace_get_port(struct device_node *dn)
  1441. {
  1442. return __cci_ace_get_port(dn, ACE_LITE_PORT);
  1443. }
  1444. EXPORT_SYMBOL_GPL(cci_ace_get_port);
  1445. static void cci_ace_init_ports(void)
  1446. {
  1447. int port, cpu;
  1448. struct device_node *cpun;
  1449. /*
  1450. * Port index look-up speeds up the function disabling ports by CPU,
  1451. * since the logical to port index mapping is done once and does
  1452. * not change after system boot.
  1453. * The stashed index array is initialized for all possible CPUs
  1454. * at probe time.
  1455. */
  1456. for_each_possible_cpu(cpu) {
  1457. /* too early to use cpu->of_node */
  1458. cpun = of_get_cpu_node(cpu, NULL);
  1459. if (WARN(!cpun, "Missing cpu device node\n"))
  1460. continue;
  1461. port = __cci_ace_get_port(cpun, ACE_PORT);
  1462. if (port < 0)
  1463. continue;
  1464. init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
  1465. }
  1466. for_each_possible_cpu(cpu) {
  1467. WARN(!cpu_port_is_valid(&cpu_port[cpu]),
  1468. "CPU %u does not have an associated CCI port\n",
  1469. cpu);
  1470. }
  1471. }
  1472. /*
  1473. * Functions to enable/disable a CCI interconnect slave port
  1474. *
  1475. * They are called by low-level power management code to disable slave
  1476. * interfaces snoops and DVM broadcast.
  1477. * Since they may execute with cache data allocation disabled and
  1478. * after the caches have been cleaned and invalidated the functions provide
  1479. * no explicit locking since they may run with D-cache disabled, so normal
  1480. * cacheable kernel locks based on ldrex/strex may not work.
  1481. * Locking has to be provided by BSP implementations to ensure proper
  1482. * operations.
  1483. */
  1484. /**
  1485. * cci_port_control() - function to control a CCI port
  1486. *
  1487. * @port: index of the port to setup
  1488. * @enable: if true enables the port, if false disables it
  1489. */
  1490. static void notrace cci_port_control(unsigned int port, bool enable)
  1491. {
  1492. void __iomem *base = ports[port].base;
  1493. writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
  1494. /*
  1495. * This function is called from power down procedures
  1496. * and must not execute any instruction that might
  1497. * cause the processor to be put in a quiescent state
  1498. * (eg wfi). Hence, cpu_relax() can not be added to this
  1499. * read loop to optimize power, since it might hide possibly
  1500. * disruptive operations.
  1501. */
  1502. while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
  1503. ;
  1504. }
  1505. /**
  1506. * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
  1507. * reference
  1508. *
  1509. * @mpidr: mpidr of the CPU whose CCI port should be disabled
  1510. *
  1511. * Disabling a CCI port for a CPU implies disabling the CCI port
  1512. * controlling that CPU cluster. Code disabling CPU CCI ports
  1513. * must make sure that the CPU running the code is the last active CPU
  1514. * in the cluster ie all other CPUs are quiescent in a low power state.
  1515. *
  1516. * Return:
  1517. * 0 on success
  1518. * -ENODEV on port look-up failure
  1519. */
  1520. int notrace cci_disable_port_by_cpu(u64 mpidr)
  1521. {
  1522. int cpu;
  1523. bool is_valid;
  1524. for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
  1525. is_valid = cpu_port_is_valid(&cpu_port[cpu]);
  1526. if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
  1527. cci_port_control(cpu_port[cpu].port, false);
  1528. return 0;
  1529. }
  1530. }
  1531. return -ENODEV;
  1532. }
  1533. EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
  1534. /**
  1535. * cci_enable_port_for_self() - enable a CCI port for calling CPU
  1536. *
  1537. * Enabling a CCI port for the calling CPU implies enabling the CCI
  1538. * port controlling that CPU's cluster. Caller must make sure that the
  1539. * CPU running the code is the first active CPU in the cluster and all
  1540. * other CPUs are quiescent in a low power state or waiting for this CPU
  1541. * to complete the CCI initialization.
  1542. *
  1543. * Because this is called when the MMU is still off and with no stack,
  1544. * the code must be position independent and ideally rely on callee
  1545. * clobbered registers only. To achieve this we must code this function
  1546. * entirely in assembler.
  1547. *
  1548. * On success this returns with the proper CCI port enabled. In case of
  1549. * any failure this never returns as the inability to enable the CCI is
  1550. * fatal and there is no possible recovery at this stage.
  1551. */
  1552. asmlinkage void __naked cci_enable_port_for_self(void)
  1553. {
  1554. asm volatile ("\n"
  1555. " .arch armv7-a\n"
  1556. " mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
  1557. " and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
  1558. " adr r1, 5f \n"
  1559. " ldr r2, [r1] \n"
  1560. " add r1, r1, r2 @ &cpu_port \n"
  1561. " add ip, r1, %[sizeof_cpu_port] \n"
  1562. /* Loop over the cpu_port array looking for a matching MPIDR */
  1563. "1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
  1564. " cmp r2, r0 @ compare MPIDR \n"
  1565. " bne 2f \n"
  1566. /* Found a match, now test port validity */
  1567. " ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
  1568. " tst r3, #"__stringify(PORT_VALID)" \n"
  1569. " bne 3f \n"
  1570. /* no match, loop with the next cpu_port entry */
  1571. "2: add r1, r1, %[sizeof_struct_cpu_port] \n"
  1572. " cmp r1, ip @ done? \n"
  1573. " blo 1b \n"
  1574. /* CCI port not found -- cheaply try to stall this CPU */
  1575. "cci_port_not_found: \n"
  1576. " wfi \n"
  1577. " wfe \n"
  1578. " b cci_port_not_found \n"
  1579. /* Use matched port index to look up the corresponding ports entry */
  1580. "3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
  1581. " adr r0, 6f \n"
  1582. " ldmia r0, {r1, r2} \n"
  1583. " sub r1, r1, r0 @ virt - phys \n"
  1584. " ldr r0, [r0, r2] @ *(&ports) \n"
  1585. " mov r2, %[sizeof_struct_ace_port] \n"
  1586. " mla r0, r2, r3, r0 @ &ports[index] \n"
  1587. " sub r0, r0, r1 @ virt_to_phys() \n"
  1588. /* Enable the CCI port */
  1589. " ldr r0, [r0, %[offsetof_port_phys]] \n"
  1590. " mov r3, %[cci_enable_req]\n"
  1591. " str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
  1592. /* poll the status reg for completion */
  1593. " adr r1, 7f \n"
  1594. " ldr r0, [r1] \n"
  1595. " ldr r0, [r0, r1] @ cci_ctrl_base \n"
  1596. "4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
  1597. " tst r1, %[cci_control_status_bits] \n"
  1598. " bne 4b \n"
  1599. " mov r0, #0 \n"
  1600. " bx lr \n"
  1601. " .align 2 \n"
  1602. "5: .word cpu_port - . \n"
  1603. "6: .word . \n"
  1604. " .word ports - 6b \n"
  1605. "7: .word cci_ctrl_phys - . \n"
  1606. : :
  1607. [sizeof_cpu_port] "i" (sizeof(cpu_port)),
  1608. [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
  1609. [cci_control_status_bits] "i" cpu_to_le32(1),
  1610. #ifndef __ARMEB__
  1611. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
  1612. #else
  1613. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
  1614. #endif
  1615. [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
  1616. [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
  1617. [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
  1618. [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
  1619. unreachable();
  1620. }
  1621. /**
  1622. * __cci_control_port_by_device() - function to control a CCI port by device
  1623. * reference
  1624. *
  1625. * @dn: device node pointer of the device whose CCI port should be
  1626. * controlled
  1627. * @enable: if true enables the port, if false disables it
  1628. *
  1629. * Return:
  1630. * 0 on success
  1631. * -ENODEV on port look-up failure
  1632. */
  1633. int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
  1634. {
  1635. int port;
  1636. if (!dn)
  1637. return -ENODEV;
  1638. port = __cci_ace_get_port(dn, ACE_LITE_PORT);
  1639. if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
  1640. dn->full_name))
  1641. return -ENODEV;
  1642. cci_port_control(port, enable);
  1643. return 0;
  1644. }
  1645. EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
  1646. /**
  1647. * __cci_control_port_by_index() - function to control a CCI port by port index
  1648. *
  1649. * @port: port index previously retrieved with cci_ace_get_port()
  1650. * @enable: if true enables the port, if false disables it
  1651. *
  1652. * Return:
  1653. * 0 on success
  1654. * -ENODEV on port index out of range
  1655. * -EPERM if operation carried out on an ACE PORT
  1656. */
  1657. int notrace __cci_control_port_by_index(u32 port, bool enable)
  1658. {
  1659. if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
  1660. return -ENODEV;
  1661. /*
  1662. * CCI control for ports connected to CPUS is extremely fragile
  1663. * and must be made to go through a specific and controlled
  1664. * interface (ie cci_disable_port_by_cpu(); control by general purpose
  1665. * indexing is therefore disabled for ACE ports.
  1666. */
  1667. if (ports[port].type == ACE_PORT)
  1668. return -EPERM;
  1669. cci_port_control(port, enable);
  1670. return 0;
  1671. }
  1672. EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
  1673. static const struct of_device_id arm_cci_ctrl_if_matches[] = {
  1674. {.compatible = "arm,cci-400-ctrl-if", },
  1675. {},
  1676. };
  1677. static int cci_probe_ports(struct device_node *np)
  1678. {
  1679. struct cci_nb_ports const *cci_config;
  1680. int ret, i, nb_ace = 0, nb_ace_lite = 0;
  1681. struct device_node *cp;
  1682. struct resource res;
  1683. const char *match_str;
  1684. bool is_ace;
  1685. cci_config = of_match_node(arm_cci_matches, np)->data;
  1686. if (!cci_config)
  1687. return -ENODEV;
  1688. nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
  1689. ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
  1690. if (!ports)
  1691. return -ENOMEM;
  1692. for_each_child_of_node(np, cp) {
  1693. if (!of_match_node(arm_cci_ctrl_if_matches, cp))
  1694. continue;
  1695. i = nb_ace + nb_ace_lite;
  1696. if (i >= nb_cci_ports)
  1697. break;
  1698. if (of_property_read_string(cp, "interface-type",
  1699. &match_str)) {
  1700. WARN(1, "node %s missing interface-type property\n",
  1701. cp->full_name);
  1702. continue;
  1703. }
  1704. is_ace = strcmp(match_str, "ace") == 0;
  1705. if (!is_ace && strcmp(match_str, "ace-lite")) {
  1706. WARN(1, "node %s containing invalid interface-type property, skipping it\n",
  1707. cp->full_name);
  1708. continue;
  1709. }
  1710. ret = of_address_to_resource(cp, 0, &res);
  1711. if (!ret) {
  1712. ports[i].base = ioremap(res.start, resource_size(&res));
  1713. ports[i].phys = res.start;
  1714. }
  1715. if (ret || !ports[i].base) {
  1716. WARN(1, "unable to ioremap CCI port %d\n", i);
  1717. continue;
  1718. }
  1719. if (is_ace) {
  1720. if (WARN_ON(nb_ace >= cci_config->nb_ace))
  1721. continue;
  1722. ports[i].type = ACE_PORT;
  1723. ++nb_ace;
  1724. } else {
  1725. if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
  1726. continue;
  1727. ports[i].type = ACE_LITE_PORT;
  1728. ++nb_ace_lite;
  1729. }
  1730. ports[i].dn = cp;
  1731. }
  1732. /* initialize a stashed array of ACE ports to speed-up look-up */
  1733. cci_ace_init_ports();
  1734. /*
  1735. * Multi-cluster systems may need this data when non-coherent, during
  1736. * cluster power-up/power-down. Make sure it reaches main memory.
  1737. */
  1738. sync_cache_w(&cci_ctrl_base);
  1739. sync_cache_w(&cci_ctrl_phys);
  1740. sync_cache_w(&ports);
  1741. sync_cache_w(&cpu_port);
  1742. __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
  1743. pr_info("ARM CCI driver probed\n");
  1744. return 0;
  1745. }
  1746. #else /* !CONFIG_ARM_CCI400_PORT_CTRL */
  1747. static inline int cci_probe_ports(struct device_node *np)
  1748. {
  1749. return 0;
  1750. }
  1751. #endif /* CONFIG_ARM_CCI400_PORT_CTRL */
  1752. static int cci_probe(void)
  1753. {
  1754. int ret;
  1755. struct device_node *np;
  1756. struct resource res;
  1757. np = of_find_matching_node(NULL, arm_cci_matches);
  1758. if(!np || !of_device_is_available(np))
  1759. return -ENODEV;
  1760. ret = of_address_to_resource(np, 0, &res);
  1761. if (!ret) {
  1762. cci_ctrl_base = ioremap(res.start, resource_size(&res));
  1763. cci_ctrl_phys = res.start;
  1764. }
  1765. if (ret || !cci_ctrl_base) {
  1766. WARN(1, "unable to ioremap CCI ctrl\n");
  1767. return -ENXIO;
  1768. }
  1769. return cci_probe_ports(np);
  1770. }
  1771. static int cci_init_status = -EAGAIN;
  1772. static DEFINE_MUTEX(cci_probing);
  1773. static int cci_init(void)
  1774. {
  1775. if (cci_init_status != -EAGAIN)
  1776. return cci_init_status;
  1777. mutex_lock(&cci_probing);
  1778. if (cci_init_status == -EAGAIN)
  1779. cci_init_status = cci_probe();
  1780. mutex_unlock(&cci_probing);
  1781. return cci_init_status;
  1782. }
  1783. /*
  1784. * To sort out early init calls ordering a helper function is provided to
  1785. * check if the CCI driver has beed initialized. Function check if the driver
  1786. * has been initialized, if not it calls the init function that probes
  1787. * the driver and updates the return value.
  1788. */
  1789. bool cci_probed(void)
  1790. {
  1791. return cci_init() == 0;
  1792. }
  1793. EXPORT_SYMBOL_GPL(cci_probed);
  1794. early_initcall(cci_init);
  1795. core_initcall(cci_platform_init);
  1796. MODULE_LICENSE("GPL");
  1797. MODULE_DESCRIPTION("ARM CCI support");