arm-cci.c 63 KB

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