arm-cci.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. // SPDX-License-Identifier: GPL-2.0
  2. // CCI Cache Coherent Interconnect PMU driver
  3. // Copyright (C) 2013-2018 Arm Ltd.
  4. // Author: Punit Agrawal <punit.agrawal@arm.com>, Suzuki Poulose <suzuki.poulose@arm.com>
  5. #include <linux/arm-cci.h>
  6. #include <linux/io.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/module.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/perf_event.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #define DRIVER_NAME "ARM-CCI PMU"
  18. #define CCI_PMCR 0x0100
  19. #define CCI_PID2 0x0fe8
  20. #define CCI_PMCR_CEN 0x00000001
  21. #define CCI_PMCR_NCNT_MASK 0x0000f800
  22. #define CCI_PMCR_NCNT_SHIFT 11
  23. #define CCI_PID2_REV_MASK 0xf0
  24. #define CCI_PID2_REV_SHIFT 4
  25. #define CCI_PMU_EVT_SEL 0x000
  26. #define CCI_PMU_CNTR 0x004
  27. #define CCI_PMU_CNTR_CTRL 0x008
  28. #define CCI_PMU_OVRFLW 0x00c
  29. #define CCI_PMU_OVRFLW_FLAG 1
  30. #define CCI_PMU_CNTR_SIZE(model) ((model)->cntr_size)
  31. #define CCI_PMU_CNTR_BASE(model, idx) ((idx) * CCI_PMU_CNTR_SIZE(model))
  32. #define CCI_PMU_CNTR_MASK ((1ULL << 32) -1)
  33. #define CCI_PMU_CNTR_LAST(cci_pmu) (cci_pmu->num_cntrs - 1)
  34. #define CCI_PMU_MAX_HW_CNTRS(model) \
  35. ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
  36. /* Types of interfaces that can generate events */
  37. enum {
  38. CCI_IF_SLAVE,
  39. CCI_IF_MASTER,
  40. #ifdef CONFIG_ARM_CCI5xx_PMU
  41. CCI_IF_GLOBAL,
  42. #endif
  43. CCI_IF_MAX,
  44. };
  45. struct event_range {
  46. u32 min;
  47. u32 max;
  48. };
  49. struct cci_pmu_hw_events {
  50. struct perf_event **events;
  51. unsigned long *used_mask;
  52. raw_spinlock_t pmu_lock;
  53. };
  54. struct cci_pmu;
  55. /*
  56. * struct cci_pmu_model:
  57. * @fixed_hw_cntrs - Number of fixed event counters
  58. * @num_hw_cntrs - Maximum number of programmable event counters
  59. * @cntr_size - Size of an event counter mapping
  60. */
  61. struct cci_pmu_model {
  62. char *name;
  63. u32 fixed_hw_cntrs;
  64. u32 num_hw_cntrs;
  65. u32 cntr_size;
  66. struct attribute **format_attrs;
  67. struct attribute **event_attrs;
  68. struct event_range event_ranges[CCI_IF_MAX];
  69. int (*validate_hw_event)(struct cci_pmu *, unsigned long);
  70. int (*get_event_idx)(struct cci_pmu *, struct cci_pmu_hw_events *, unsigned long);
  71. void (*write_counters)(struct cci_pmu *, unsigned long *);
  72. };
  73. static struct cci_pmu_model cci_pmu_models[];
  74. struct cci_pmu {
  75. void __iomem *base;
  76. void __iomem *ctrl_base;
  77. struct pmu pmu;
  78. int cpu;
  79. int nr_irqs;
  80. int *irqs;
  81. unsigned long active_irqs;
  82. const struct cci_pmu_model *model;
  83. struct cci_pmu_hw_events hw_events;
  84. struct platform_device *plat_device;
  85. int num_cntrs;
  86. atomic_t active_events;
  87. struct mutex reserve_mutex;
  88. };
  89. #define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
  90. static struct cci_pmu *g_cci_pmu;
  91. enum cci_models {
  92. #ifdef CONFIG_ARM_CCI400_PMU
  93. CCI400_R0,
  94. CCI400_R1,
  95. #endif
  96. #ifdef CONFIG_ARM_CCI5xx_PMU
  97. CCI500_R0,
  98. CCI550_R0,
  99. #endif
  100. CCI_MODEL_MAX
  101. };
  102. static void pmu_write_counters(struct cci_pmu *cci_pmu,
  103. unsigned long *mask);
  104. static ssize_t cci_pmu_format_show(struct device *dev,
  105. struct device_attribute *attr, char *buf);
  106. static ssize_t cci_pmu_event_show(struct device *dev,
  107. struct device_attribute *attr, char *buf);
  108. #define CCI_EXT_ATTR_ENTRY(_name, _func, _config) \
  109. &((struct dev_ext_attribute[]) { \
  110. { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config } \
  111. })[0].attr.attr
  112. #define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
  113. CCI_EXT_ATTR_ENTRY(_name, cci_pmu_format_show, (char *)_config)
  114. #define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
  115. CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
  116. /* CCI400 PMU Specific definitions */
  117. #ifdef CONFIG_ARM_CCI400_PMU
  118. /* Port ids */
  119. #define CCI400_PORT_S0 0
  120. #define CCI400_PORT_S1 1
  121. #define CCI400_PORT_S2 2
  122. #define CCI400_PORT_S3 3
  123. #define CCI400_PORT_S4 4
  124. #define CCI400_PORT_M0 5
  125. #define CCI400_PORT_M1 6
  126. #define CCI400_PORT_M2 7
  127. #define CCI400_R1_PX 5
  128. /*
  129. * Instead of an event id to monitor CCI cycles, a dedicated counter is
  130. * provided. Use 0xff to represent CCI cycles and hope that no future revisions
  131. * make use of this event in hardware.
  132. */
  133. enum cci400_perf_events {
  134. CCI400_PMU_CYCLES = 0xff
  135. };
  136. #define CCI400_PMU_CYCLE_CNTR_IDX 0
  137. #define CCI400_PMU_CNTR0_IDX 1
  138. /*
  139. * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
  140. * ports and bits 4:0 are event codes. There are different event codes
  141. * associated with each port type.
  142. *
  143. * Additionally, the range of events associated with the port types changed
  144. * between Rev0 and Rev1.
  145. *
  146. * The constants below define the range of valid codes for each port type for
  147. * the different revisions and are used to validate the event to be monitored.
  148. */
  149. #define CCI400_PMU_EVENT_MASK 0xffUL
  150. #define CCI400_PMU_EVENT_SOURCE_SHIFT 5
  151. #define CCI400_PMU_EVENT_SOURCE_MASK 0x7
  152. #define CCI400_PMU_EVENT_CODE_SHIFT 0
  153. #define CCI400_PMU_EVENT_CODE_MASK 0x1f
  154. #define CCI400_PMU_EVENT_SOURCE(event) \
  155. ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
  156. CCI400_PMU_EVENT_SOURCE_MASK)
  157. #define CCI400_PMU_EVENT_CODE(event) \
  158. ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
  159. #define CCI400_R0_SLAVE_PORT_MIN_EV 0x00
  160. #define CCI400_R0_SLAVE_PORT_MAX_EV 0x13
  161. #define CCI400_R0_MASTER_PORT_MIN_EV 0x14
  162. #define CCI400_R0_MASTER_PORT_MAX_EV 0x1a
  163. #define CCI400_R1_SLAVE_PORT_MIN_EV 0x00
  164. #define CCI400_R1_SLAVE_PORT_MAX_EV 0x14
  165. #define CCI400_R1_MASTER_PORT_MIN_EV 0x00
  166. #define CCI400_R1_MASTER_PORT_MAX_EV 0x11
  167. #define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
  168. CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
  169. (unsigned long)_config)
  170. static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
  171. struct device_attribute *attr, char *buf);
  172. static struct attribute *cci400_pmu_format_attrs[] = {
  173. CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
  174. CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-7"),
  175. NULL
  176. };
  177. static struct attribute *cci400_r0_pmu_event_attrs[] = {
  178. /* Slave events */
  179. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
  180. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
  181. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
  182. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
  183. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
  184. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
  185. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
  186. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
  187. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
  188. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
  189. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
  190. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
  191. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
  192. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
  193. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
  194. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
  195. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
  196. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
  197. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
  198. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
  199. /* Master events */
  200. CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x14),
  201. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard, 0x15),
  202. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard, 0x16),
  203. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full, 0x17),
  204. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x18),
  205. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x19),
  206. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full, 0x1A),
  207. /* Special event for cycles counter */
  208. CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
  209. NULL
  210. };
  211. static struct attribute *cci400_r1_pmu_event_attrs[] = {
  212. /* Slave events */
  213. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
  214. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
  215. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
  216. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
  217. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
  218. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
  219. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
  220. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
  221. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
  222. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
  223. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
  224. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
  225. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
  226. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
  227. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
  228. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
  229. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
  230. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
  231. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
  232. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
  233. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard, 0x14),
  234. /* Master events */
  235. CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x0),
  236. CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard, 0x1),
  237. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard, 0x2),
  238. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full, 0x3),
  239. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x4),
  240. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x5),
  241. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full, 0x6),
  242. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full, 0x7),
  243. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full, 0x8),
  244. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0, 0x9),
  245. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1, 0xA),
  246. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2, 0xB),
  247. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3, 0xC),
  248. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0, 0xD),
  249. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1, 0xE),
  250. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2, 0xF),
  251. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3, 0x10),
  252. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard, 0x11),
  253. /* Special event for cycles counter */
  254. CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
  255. NULL
  256. };
  257. static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. struct dev_ext_attribute *eattr = container_of(attr,
  261. struct dev_ext_attribute, attr);
  262. return snprintf(buf, PAGE_SIZE, "config=0x%lx\n", (unsigned long)eattr->var);
  263. }
  264. static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
  265. struct cci_pmu_hw_events *hw,
  266. unsigned long cci_event)
  267. {
  268. int idx;
  269. /* cycles event idx is fixed */
  270. if (cci_event == CCI400_PMU_CYCLES) {
  271. if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
  272. return -EAGAIN;
  273. return CCI400_PMU_CYCLE_CNTR_IDX;
  274. }
  275. for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
  276. if (!test_and_set_bit(idx, hw->used_mask))
  277. return idx;
  278. /* No counters available */
  279. return -EAGAIN;
  280. }
  281. static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
  282. {
  283. u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
  284. u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
  285. int if_type;
  286. if (hw_event & ~CCI400_PMU_EVENT_MASK)
  287. return -ENOENT;
  288. if (hw_event == CCI400_PMU_CYCLES)
  289. return hw_event;
  290. switch (ev_source) {
  291. case CCI400_PORT_S0:
  292. case CCI400_PORT_S1:
  293. case CCI400_PORT_S2:
  294. case CCI400_PORT_S3:
  295. case CCI400_PORT_S4:
  296. /* Slave Interface */
  297. if_type = CCI_IF_SLAVE;
  298. break;
  299. case CCI400_PORT_M0:
  300. case CCI400_PORT_M1:
  301. case CCI400_PORT_M2:
  302. /* Master Interface */
  303. if_type = CCI_IF_MASTER;
  304. break;
  305. default:
  306. return -ENOENT;
  307. }
  308. if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
  309. ev_code <= cci_pmu->model->event_ranges[if_type].max)
  310. return hw_event;
  311. return -ENOENT;
  312. }
  313. static int probe_cci400_revision(struct cci_pmu *cci_pmu)
  314. {
  315. int rev;
  316. rev = readl_relaxed(cci_pmu->ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
  317. rev >>= CCI_PID2_REV_SHIFT;
  318. if (rev < CCI400_R1_PX)
  319. return CCI400_R0;
  320. else
  321. return CCI400_R1;
  322. }
  323. static const struct cci_pmu_model *probe_cci_model(struct cci_pmu *cci_pmu)
  324. {
  325. if (platform_has_secure_cci_access())
  326. return &cci_pmu_models[probe_cci400_revision(cci_pmu)];
  327. return NULL;
  328. }
  329. #else /* !CONFIG_ARM_CCI400_PMU */
  330. static inline struct cci_pmu_model *probe_cci_model(struct cci_pmu *cci_pmu)
  331. {
  332. return NULL;
  333. }
  334. #endif /* CONFIG_ARM_CCI400_PMU */
  335. #ifdef CONFIG_ARM_CCI5xx_PMU
  336. /*
  337. * CCI5xx PMU event id is an 9-bit value made of two parts.
  338. * bits [8:5] - Source for the event
  339. * bits [4:0] - Event code (specific to type of interface)
  340. *
  341. *
  342. */
  343. /* Port ids */
  344. #define CCI5xx_PORT_S0 0x0
  345. #define CCI5xx_PORT_S1 0x1
  346. #define CCI5xx_PORT_S2 0x2
  347. #define CCI5xx_PORT_S3 0x3
  348. #define CCI5xx_PORT_S4 0x4
  349. #define CCI5xx_PORT_S5 0x5
  350. #define CCI5xx_PORT_S6 0x6
  351. #define CCI5xx_PORT_M0 0x8
  352. #define CCI5xx_PORT_M1 0x9
  353. #define CCI5xx_PORT_M2 0xa
  354. #define CCI5xx_PORT_M3 0xb
  355. #define CCI5xx_PORT_M4 0xc
  356. #define CCI5xx_PORT_M5 0xd
  357. #define CCI5xx_PORT_M6 0xe
  358. #define CCI5xx_PORT_GLOBAL 0xf
  359. #define CCI5xx_PMU_EVENT_MASK 0x1ffUL
  360. #define CCI5xx_PMU_EVENT_SOURCE_SHIFT 0x5
  361. #define CCI5xx_PMU_EVENT_SOURCE_MASK 0xf
  362. #define CCI5xx_PMU_EVENT_CODE_SHIFT 0x0
  363. #define CCI5xx_PMU_EVENT_CODE_MASK 0x1f
  364. #define CCI5xx_PMU_EVENT_SOURCE(event) \
  365. ((event >> CCI5xx_PMU_EVENT_SOURCE_SHIFT) & CCI5xx_PMU_EVENT_SOURCE_MASK)
  366. #define CCI5xx_PMU_EVENT_CODE(event) \
  367. ((event >> CCI5xx_PMU_EVENT_CODE_SHIFT) & CCI5xx_PMU_EVENT_CODE_MASK)
  368. #define CCI5xx_SLAVE_PORT_MIN_EV 0x00
  369. #define CCI5xx_SLAVE_PORT_MAX_EV 0x1f
  370. #define CCI5xx_MASTER_PORT_MIN_EV 0x00
  371. #define CCI5xx_MASTER_PORT_MAX_EV 0x06
  372. #define CCI5xx_GLOBAL_PORT_MIN_EV 0x00
  373. #define CCI5xx_GLOBAL_PORT_MAX_EV 0x0f
  374. #define CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
  375. CCI_EXT_ATTR_ENTRY(_name, cci5xx_pmu_global_event_show, \
  376. (unsigned long) _config)
  377. static ssize_t cci5xx_pmu_global_event_show(struct device *dev,
  378. struct device_attribute *attr, char *buf);
  379. static struct attribute *cci5xx_pmu_format_attrs[] = {
  380. CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
  381. CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-8"),
  382. NULL,
  383. };
  384. static struct attribute *cci5xx_pmu_event_attrs[] = {
  385. /* Slave events */
  386. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_arvalid, 0x0),
  387. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_dev, 0x1),
  388. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_nonshareable, 0x2),
  389. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_non_alloc, 0x3),
  390. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_alloc, 0x4),
  391. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_invalidate, 0x5),
  392. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maint, 0x6),
  393. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
  394. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rval, 0x8),
  395. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rlast_snoop, 0x9),
  396. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_awalid, 0xA),
  397. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_dev, 0xB),
  398. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_non_shareable, 0xC),
  399. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wb, 0xD),
  400. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wlu, 0xE),
  401. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wunique, 0xF),
  402. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_evict, 0x10),
  403. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_wrevict, 0x11),
  404. CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_beat, 0x12),
  405. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_acvalid, 0x13),
  406. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_read, 0x14),
  407. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_clean, 0x15),
  408. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_data_transfer_low, 0x16),
  409. CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_arvalid, 0x17),
  410. CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall, 0x18),
  411. CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall, 0x19),
  412. CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_stall, 0x1A),
  413. CCI_EVENT_EXT_ATTR_ENTRY(si_w_resp_stall, 0x1B),
  414. CCI_EVENT_EXT_ATTR_ENTRY(si_srq_stall, 0x1C),
  415. CCI_EVENT_EXT_ATTR_ENTRY(si_s_data_stall, 0x1D),
  416. CCI_EVENT_EXT_ATTR_ENTRY(si_rq_stall_ot_limit, 0x1E),
  417. CCI_EVENT_EXT_ATTR_ENTRY(si_r_stall_arbit, 0x1F),
  418. /* Master events */
  419. CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_beat_any, 0x0),
  420. CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_beat_any, 0x1),
  421. CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall, 0x2),
  422. CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_stall, 0x3),
  423. CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall, 0x4),
  424. CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_stall, 0x5),
  425. CCI_EVENT_EXT_ATTR_ENTRY(mi_w_resp_stall, 0x6),
  426. /* Global events */
  427. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1, 0x0),
  428. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3, 0x1),
  429. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5, 0x2),
  430. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7, 0x3),
  431. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1, 0x4),
  432. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3, 0x5),
  433. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5, 0x6),
  434. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7, 0x7),
  435. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation, 0x8),
  436. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy, 0x9),
  437. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full, 0xA),
  438. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq, 0xB),
  439. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs, 0xC),
  440. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard, 0xD),
  441. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_stall_tt_full, 0xE),
  442. CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot, 0xF),
  443. NULL
  444. };
  445. static ssize_t cci5xx_pmu_global_event_show(struct device *dev,
  446. struct device_attribute *attr, char *buf)
  447. {
  448. struct dev_ext_attribute *eattr = container_of(attr,
  449. struct dev_ext_attribute, attr);
  450. /* Global events have single fixed source code */
  451. return snprintf(buf, PAGE_SIZE, "event=0x%lx,source=0x%x\n",
  452. (unsigned long)eattr->var, CCI5xx_PORT_GLOBAL);
  453. }
  454. /*
  455. * CCI500 provides 8 independent event counters that can count
  456. * any of the events available.
  457. * CCI500 PMU event source ids
  458. * 0x0-0x6 - Slave interfaces
  459. * 0x8-0xD - Master interfaces
  460. * 0xf - Global Events
  461. * 0x7,0xe - Reserved
  462. */
  463. static int cci500_validate_hw_event(struct cci_pmu *cci_pmu,
  464. unsigned long hw_event)
  465. {
  466. u32 ev_source = CCI5xx_PMU_EVENT_SOURCE(hw_event);
  467. u32 ev_code = CCI5xx_PMU_EVENT_CODE(hw_event);
  468. int if_type;
  469. if (hw_event & ~CCI5xx_PMU_EVENT_MASK)
  470. return -ENOENT;
  471. switch (ev_source) {
  472. case CCI5xx_PORT_S0:
  473. case CCI5xx_PORT_S1:
  474. case CCI5xx_PORT_S2:
  475. case CCI5xx_PORT_S3:
  476. case CCI5xx_PORT_S4:
  477. case CCI5xx_PORT_S5:
  478. case CCI5xx_PORT_S6:
  479. if_type = CCI_IF_SLAVE;
  480. break;
  481. case CCI5xx_PORT_M0:
  482. case CCI5xx_PORT_M1:
  483. case CCI5xx_PORT_M2:
  484. case CCI5xx_PORT_M3:
  485. case CCI5xx_PORT_M4:
  486. case CCI5xx_PORT_M5:
  487. if_type = CCI_IF_MASTER;
  488. break;
  489. case CCI5xx_PORT_GLOBAL:
  490. if_type = CCI_IF_GLOBAL;
  491. break;
  492. default:
  493. return -ENOENT;
  494. }
  495. if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
  496. ev_code <= cci_pmu->model->event_ranges[if_type].max)
  497. return hw_event;
  498. return -ENOENT;
  499. }
  500. /*
  501. * CCI550 provides 8 independent event counters that can count
  502. * any of the events available.
  503. * CCI550 PMU event source ids
  504. * 0x0-0x6 - Slave interfaces
  505. * 0x8-0xe - Master interfaces
  506. * 0xf - Global Events
  507. * 0x7 - Reserved
  508. */
  509. static int cci550_validate_hw_event(struct cci_pmu *cci_pmu,
  510. unsigned long hw_event)
  511. {
  512. u32 ev_source = CCI5xx_PMU_EVENT_SOURCE(hw_event);
  513. u32 ev_code = CCI5xx_PMU_EVENT_CODE(hw_event);
  514. int if_type;
  515. if (hw_event & ~CCI5xx_PMU_EVENT_MASK)
  516. return -ENOENT;
  517. switch (ev_source) {
  518. case CCI5xx_PORT_S0:
  519. case CCI5xx_PORT_S1:
  520. case CCI5xx_PORT_S2:
  521. case CCI5xx_PORT_S3:
  522. case CCI5xx_PORT_S4:
  523. case CCI5xx_PORT_S5:
  524. case CCI5xx_PORT_S6:
  525. if_type = CCI_IF_SLAVE;
  526. break;
  527. case CCI5xx_PORT_M0:
  528. case CCI5xx_PORT_M1:
  529. case CCI5xx_PORT_M2:
  530. case CCI5xx_PORT_M3:
  531. case CCI5xx_PORT_M4:
  532. case CCI5xx_PORT_M5:
  533. case CCI5xx_PORT_M6:
  534. if_type = CCI_IF_MASTER;
  535. break;
  536. case CCI5xx_PORT_GLOBAL:
  537. if_type = CCI_IF_GLOBAL;
  538. break;
  539. default:
  540. return -ENOENT;
  541. }
  542. if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
  543. ev_code <= cci_pmu->model->event_ranges[if_type].max)
  544. return hw_event;
  545. return -ENOENT;
  546. }
  547. #endif /* CONFIG_ARM_CCI5xx_PMU */
  548. /*
  549. * Program the CCI PMU counters which have PERF_HES_ARCH set
  550. * with the event period and mark them ready before we enable
  551. * PMU.
  552. */
  553. static void cci_pmu_sync_counters(struct cci_pmu *cci_pmu)
  554. {
  555. int i;
  556. struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
  557. DECLARE_BITMAP(mask, cci_pmu->num_cntrs);
  558. bitmap_zero(mask, cci_pmu->num_cntrs);
  559. for_each_set_bit(i, cci_pmu->hw_events.used_mask, cci_pmu->num_cntrs) {
  560. struct perf_event *event = cci_hw->events[i];
  561. if (WARN_ON(!event))
  562. continue;
  563. /* Leave the events which are not counting */
  564. if (event->hw.state & PERF_HES_STOPPED)
  565. continue;
  566. if (event->hw.state & PERF_HES_ARCH) {
  567. set_bit(i, mask);
  568. event->hw.state &= ~PERF_HES_ARCH;
  569. }
  570. }
  571. pmu_write_counters(cci_pmu, mask);
  572. }
  573. /* Should be called with cci_pmu->hw_events->pmu_lock held */
  574. static void __cci_pmu_enable_nosync(struct cci_pmu *cci_pmu)
  575. {
  576. u32 val;
  577. /* Enable all the PMU counters. */
  578. val = readl_relaxed(cci_pmu->ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
  579. writel(val, cci_pmu->ctrl_base + CCI_PMCR);
  580. }
  581. /* Should be called with cci_pmu->hw_events->pmu_lock held */
  582. static void __cci_pmu_enable_sync(struct cci_pmu *cci_pmu)
  583. {
  584. cci_pmu_sync_counters(cci_pmu);
  585. __cci_pmu_enable_nosync(cci_pmu);
  586. }
  587. /* Should be called with cci_pmu->hw_events->pmu_lock held */
  588. static void __cci_pmu_disable(struct cci_pmu *cci_pmu)
  589. {
  590. u32 val;
  591. /* Disable all the PMU counters. */
  592. val = readl_relaxed(cci_pmu->ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
  593. writel(val, cci_pmu->ctrl_base + CCI_PMCR);
  594. }
  595. static ssize_t cci_pmu_format_show(struct device *dev,
  596. struct device_attribute *attr, char *buf)
  597. {
  598. struct dev_ext_attribute *eattr = container_of(attr,
  599. struct dev_ext_attribute, attr);
  600. return snprintf(buf, PAGE_SIZE, "%s\n", (char *)eattr->var);
  601. }
  602. static ssize_t cci_pmu_event_show(struct device *dev,
  603. struct device_attribute *attr, char *buf)
  604. {
  605. struct dev_ext_attribute *eattr = container_of(attr,
  606. struct dev_ext_attribute, attr);
  607. /* source parameter is mandatory for normal PMU events */
  608. return snprintf(buf, PAGE_SIZE, "source=?,event=0x%lx\n",
  609. (unsigned long)eattr->var);
  610. }
  611. static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
  612. {
  613. return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
  614. }
  615. static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
  616. {
  617. return readl_relaxed(cci_pmu->base +
  618. CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
  619. }
  620. static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
  621. int idx, unsigned int offset)
  622. {
  623. writel_relaxed(value, cci_pmu->base +
  624. CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
  625. }
  626. static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
  627. {
  628. pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
  629. }
  630. static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
  631. {
  632. pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
  633. }
  634. static bool __maybe_unused
  635. pmu_counter_is_enabled(struct cci_pmu *cci_pmu, int idx)
  636. {
  637. return (pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR_CTRL) & 0x1) != 0;
  638. }
  639. static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
  640. {
  641. pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
  642. }
  643. /*
  644. * For all counters on the CCI-PMU, disable any 'enabled' counters,
  645. * saving the changed counters in the mask, so that we can restore
  646. * it later using pmu_restore_counters. The mask is private to the
  647. * caller. We cannot rely on the used_mask maintained by the CCI_PMU
  648. * as it only tells us if the counter is assigned to perf_event or not.
  649. * The state of the perf_event cannot be locked by the PMU layer, hence
  650. * we check the individual counter status (which can be locked by
  651. * cci_pm->hw_events->pmu_lock).
  652. *
  653. * @mask should be initialised to empty by the caller.
  654. */
  655. static void __maybe_unused
  656. pmu_save_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
  657. {
  658. int i;
  659. for (i = 0; i < cci_pmu->num_cntrs; i++) {
  660. if (pmu_counter_is_enabled(cci_pmu, i)) {
  661. set_bit(i, mask);
  662. pmu_disable_counter(cci_pmu, i);
  663. }
  664. }
  665. }
  666. /*
  667. * Restore the status of the counters. Reversal of the pmu_save_counters().
  668. * For each counter set in the mask, enable the counter back.
  669. */
  670. static void __maybe_unused
  671. pmu_restore_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
  672. {
  673. int i;
  674. for_each_set_bit(i, mask, cci_pmu->num_cntrs)
  675. pmu_enable_counter(cci_pmu, i);
  676. }
  677. /*
  678. * Returns the number of programmable counters actually implemented
  679. * by the cci
  680. */
  681. static u32 pmu_get_max_counters(struct cci_pmu *cci_pmu)
  682. {
  683. return (readl_relaxed(cci_pmu->ctrl_base + CCI_PMCR) &
  684. CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
  685. }
  686. static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
  687. {
  688. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  689. unsigned long cci_event = event->hw.config_base;
  690. int idx;
  691. if (cci_pmu->model->get_event_idx)
  692. return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
  693. /* Generic code to find an unused idx from the mask */
  694. for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
  695. if (!test_and_set_bit(idx, hw->used_mask))
  696. return idx;
  697. /* No counters available */
  698. return -EAGAIN;
  699. }
  700. static int pmu_map_event(struct perf_event *event)
  701. {
  702. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  703. if (event->attr.type < PERF_TYPE_MAX ||
  704. !cci_pmu->model->validate_hw_event)
  705. return -ENOENT;
  706. return cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
  707. }
  708. static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
  709. {
  710. int i;
  711. struct platform_device *pmu_device = cci_pmu->plat_device;
  712. if (unlikely(!pmu_device))
  713. return -ENODEV;
  714. if (cci_pmu->nr_irqs < 1) {
  715. dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
  716. return -ENODEV;
  717. }
  718. /*
  719. * Register all available CCI PMU interrupts. In the interrupt handler
  720. * we iterate over the counters checking for interrupt source (the
  721. * overflowing counter) and clear it.
  722. *
  723. * This should allow handling of non-unique interrupt for the counters.
  724. */
  725. for (i = 0; i < cci_pmu->nr_irqs; i++) {
  726. int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
  727. "arm-cci-pmu", cci_pmu);
  728. if (err) {
  729. dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
  730. cci_pmu->irqs[i]);
  731. return err;
  732. }
  733. set_bit(i, &cci_pmu->active_irqs);
  734. }
  735. return 0;
  736. }
  737. static void pmu_free_irq(struct cci_pmu *cci_pmu)
  738. {
  739. int i;
  740. for (i = 0; i < cci_pmu->nr_irqs; i++) {
  741. if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
  742. continue;
  743. free_irq(cci_pmu->irqs[i], cci_pmu);
  744. }
  745. }
  746. static u32 pmu_read_counter(struct perf_event *event)
  747. {
  748. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  749. struct hw_perf_event *hw_counter = &event->hw;
  750. int idx = hw_counter->idx;
  751. u32 value;
  752. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  753. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  754. return 0;
  755. }
  756. value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
  757. return value;
  758. }
  759. static void pmu_write_counter(struct cci_pmu *cci_pmu, u32 value, int idx)
  760. {
  761. pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
  762. }
  763. static void __pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
  764. {
  765. int i;
  766. struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
  767. for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
  768. struct perf_event *event = cci_hw->events[i];
  769. if (WARN_ON(!event))
  770. continue;
  771. pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
  772. }
  773. }
  774. static void pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
  775. {
  776. if (cci_pmu->model->write_counters)
  777. cci_pmu->model->write_counters(cci_pmu, mask);
  778. else
  779. __pmu_write_counters(cci_pmu, mask);
  780. }
  781. #ifdef CONFIG_ARM_CCI5xx_PMU
  782. /*
  783. * CCI-500/CCI-550 has advanced power saving policies, which could gate the
  784. * clocks to the PMU counters, which makes the writes to them ineffective.
  785. * The only way to write to those counters is when the global counters
  786. * are enabled and the particular counter is enabled.
  787. *
  788. * So we do the following :
  789. *
  790. * 1) Disable all the PMU counters, saving their current state
  791. * 2) Enable the global PMU profiling, now that all counters are
  792. * disabled.
  793. *
  794. * For each counter to be programmed, repeat steps 3-7:
  795. *
  796. * 3) Write an invalid event code to the event control register for the
  797. counter, so that the counters are not modified.
  798. * 4) Enable the counter control for the counter.
  799. * 5) Set the counter value
  800. * 6) Disable the counter
  801. * 7) Restore the event in the target counter
  802. *
  803. * 8) Disable the global PMU.
  804. * 9) Restore the status of the rest of the counters.
  805. *
  806. * We choose an event which for CCI-5xx is guaranteed not to count.
  807. * We use the highest possible event code (0x1f) for the master interface 0.
  808. */
  809. #define CCI5xx_INVALID_EVENT ((CCI5xx_PORT_M0 << CCI5xx_PMU_EVENT_SOURCE_SHIFT) | \
  810. (CCI5xx_PMU_EVENT_CODE_MASK << CCI5xx_PMU_EVENT_CODE_SHIFT))
  811. static void cci5xx_pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
  812. {
  813. int i;
  814. DECLARE_BITMAP(saved_mask, cci_pmu->num_cntrs);
  815. bitmap_zero(saved_mask, cci_pmu->num_cntrs);
  816. pmu_save_counters(cci_pmu, saved_mask);
  817. /*
  818. * Now that all the counters are disabled, we can safely turn the PMU on,
  819. * without syncing the status of the counters
  820. */
  821. __cci_pmu_enable_nosync(cci_pmu);
  822. for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
  823. struct perf_event *event = cci_pmu->hw_events.events[i];
  824. if (WARN_ON(!event))
  825. continue;
  826. pmu_set_event(cci_pmu, i, CCI5xx_INVALID_EVENT);
  827. pmu_enable_counter(cci_pmu, i);
  828. pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
  829. pmu_disable_counter(cci_pmu, i);
  830. pmu_set_event(cci_pmu, i, event->hw.config_base);
  831. }
  832. __cci_pmu_disable(cci_pmu);
  833. pmu_restore_counters(cci_pmu, saved_mask);
  834. }
  835. #endif /* CONFIG_ARM_CCI5xx_PMU */
  836. static u64 pmu_event_update(struct perf_event *event)
  837. {
  838. struct hw_perf_event *hwc = &event->hw;
  839. u64 delta, prev_raw_count, new_raw_count;
  840. do {
  841. prev_raw_count = local64_read(&hwc->prev_count);
  842. new_raw_count = pmu_read_counter(event);
  843. } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  844. new_raw_count) != prev_raw_count);
  845. delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
  846. local64_add(delta, &event->count);
  847. return new_raw_count;
  848. }
  849. static void pmu_read(struct perf_event *event)
  850. {
  851. pmu_event_update(event);
  852. }
  853. static void pmu_event_set_period(struct perf_event *event)
  854. {
  855. struct hw_perf_event *hwc = &event->hw;
  856. /*
  857. * The CCI PMU counters have a period of 2^32. To account for the
  858. * possiblity of extreme interrupt latency we program for a period of
  859. * half that. Hopefully we can handle the interrupt before another 2^31
  860. * events occur and the counter overtakes its previous value.
  861. */
  862. u64 val = 1ULL << 31;
  863. local64_set(&hwc->prev_count, val);
  864. /*
  865. * CCI PMU uses PERF_HES_ARCH to keep track of the counters, whose
  866. * values needs to be sync-ed with the s/w state before the PMU is
  867. * enabled.
  868. * Mark this counter for sync.
  869. */
  870. hwc->state |= PERF_HES_ARCH;
  871. }
  872. static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
  873. {
  874. unsigned long flags;
  875. struct cci_pmu *cci_pmu = dev;
  876. struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
  877. int idx, handled = IRQ_NONE;
  878. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  879. /* Disable the PMU while we walk through the counters */
  880. __cci_pmu_disable(cci_pmu);
  881. /*
  882. * Iterate over counters and update the corresponding perf events.
  883. * This should work regardless of whether we have per-counter overflow
  884. * interrupt or a combined overflow interrupt.
  885. */
  886. for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
  887. struct perf_event *event = events->events[idx];
  888. if (!event)
  889. continue;
  890. /* Did this counter overflow? */
  891. if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
  892. CCI_PMU_OVRFLW_FLAG))
  893. continue;
  894. pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
  895. CCI_PMU_OVRFLW);
  896. pmu_event_update(event);
  897. pmu_event_set_period(event);
  898. handled = IRQ_HANDLED;
  899. }
  900. /* Enable the PMU and sync possibly overflowed counters */
  901. __cci_pmu_enable_sync(cci_pmu);
  902. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  903. return IRQ_RETVAL(handled);
  904. }
  905. static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
  906. {
  907. int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
  908. if (ret) {
  909. pmu_free_irq(cci_pmu);
  910. return ret;
  911. }
  912. return 0;
  913. }
  914. static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
  915. {
  916. pmu_free_irq(cci_pmu);
  917. }
  918. static void hw_perf_event_destroy(struct perf_event *event)
  919. {
  920. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  921. atomic_t *active_events = &cci_pmu->active_events;
  922. struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
  923. if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
  924. cci_pmu_put_hw(cci_pmu);
  925. mutex_unlock(reserve_mutex);
  926. }
  927. }
  928. static void cci_pmu_enable(struct pmu *pmu)
  929. {
  930. struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
  931. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  932. int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
  933. unsigned long flags;
  934. if (!enabled)
  935. return;
  936. raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
  937. __cci_pmu_enable_sync(cci_pmu);
  938. raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
  939. }
  940. static void cci_pmu_disable(struct pmu *pmu)
  941. {
  942. struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
  943. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  944. unsigned long flags;
  945. raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
  946. __cci_pmu_disable(cci_pmu);
  947. raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
  948. }
  949. /*
  950. * Check if the idx represents a non-programmable counter.
  951. * All the fixed event counters are mapped before the programmable
  952. * counters.
  953. */
  954. static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
  955. {
  956. return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
  957. }
  958. static void cci_pmu_start(struct perf_event *event, int pmu_flags)
  959. {
  960. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  961. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  962. struct hw_perf_event *hwc = &event->hw;
  963. int idx = hwc->idx;
  964. unsigned long flags;
  965. /*
  966. * To handle interrupt latency, we always reprogram the period
  967. * regardlesss of PERF_EF_RELOAD.
  968. */
  969. if (pmu_flags & PERF_EF_RELOAD)
  970. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  971. hwc->state = 0;
  972. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  973. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  974. return;
  975. }
  976. raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
  977. /* Configure the counter unless you are counting a fixed event */
  978. if (!pmu_fixed_hw_idx(cci_pmu, idx))
  979. pmu_set_event(cci_pmu, idx, hwc->config_base);
  980. pmu_event_set_period(event);
  981. pmu_enable_counter(cci_pmu, idx);
  982. raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
  983. }
  984. static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
  985. {
  986. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  987. struct hw_perf_event *hwc = &event->hw;
  988. int idx = hwc->idx;
  989. if (hwc->state & PERF_HES_STOPPED)
  990. return;
  991. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  992. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  993. return;
  994. }
  995. /*
  996. * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
  997. * cci_pmu_start()
  998. */
  999. pmu_disable_counter(cci_pmu, idx);
  1000. pmu_event_update(event);
  1001. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  1002. }
  1003. static int cci_pmu_add(struct perf_event *event, int flags)
  1004. {
  1005. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  1006. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  1007. struct hw_perf_event *hwc = &event->hw;
  1008. int idx;
  1009. int err = 0;
  1010. perf_pmu_disable(event->pmu);
  1011. /* If we don't have a space for the counter then finish early. */
  1012. idx = pmu_get_event_idx(hw_events, event);
  1013. if (idx < 0) {
  1014. err = idx;
  1015. goto out;
  1016. }
  1017. event->hw.idx = idx;
  1018. hw_events->events[idx] = event;
  1019. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  1020. if (flags & PERF_EF_START)
  1021. cci_pmu_start(event, PERF_EF_RELOAD);
  1022. /* Propagate our changes to the userspace mapping. */
  1023. perf_event_update_userpage(event);
  1024. out:
  1025. perf_pmu_enable(event->pmu);
  1026. return err;
  1027. }
  1028. static void cci_pmu_del(struct perf_event *event, int flags)
  1029. {
  1030. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  1031. struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
  1032. struct hw_perf_event *hwc = &event->hw;
  1033. int idx = hwc->idx;
  1034. cci_pmu_stop(event, PERF_EF_UPDATE);
  1035. hw_events->events[idx] = NULL;
  1036. clear_bit(idx, hw_events->used_mask);
  1037. perf_event_update_userpage(event);
  1038. }
  1039. static int validate_event(struct pmu *cci_pmu,
  1040. struct cci_pmu_hw_events *hw_events,
  1041. struct perf_event *event)
  1042. {
  1043. if (is_software_event(event))
  1044. return 1;
  1045. /*
  1046. * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
  1047. * core perf code won't check that the pmu->ctx == leader->ctx
  1048. * until after pmu->event_init(event).
  1049. */
  1050. if (event->pmu != cci_pmu)
  1051. return 0;
  1052. if (event->state < PERF_EVENT_STATE_OFF)
  1053. return 1;
  1054. if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
  1055. return 1;
  1056. return pmu_get_event_idx(hw_events, event) >= 0;
  1057. }
  1058. static int validate_group(struct perf_event *event)
  1059. {
  1060. struct perf_event *sibling, *leader = event->group_leader;
  1061. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  1062. unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
  1063. struct cci_pmu_hw_events fake_pmu = {
  1064. /*
  1065. * Initialise the fake PMU. We only need to populate the
  1066. * used_mask for the purposes of validation.
  1067. */
  1068. .used_mask = mask,
  1069. };
  1070. memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
  1071. if (!validate_event(event->pmu, &fake_pmu, leader))
  1072. return -EINVAL;
  1073. for_each_sibling_event(sibling, leader) {
  1074. if (!validate_event(event->pmu, &fake_pmu, sibling))
  1075. return -EINVAL;
  1076. }
  1077. if (!validate_event(event->pmu, &fake_pmu, event))
  1078. return -EINVAL;
  1079. return 0;
  1080. }
  1081. static int __hw_perf_event_init(struct perf_event *event)
  1082. {
  1083. struct hw_perf_event *hwc = &event->hw;
  1084. int mapping;
  1085. mapping = pmu_map_event(event);
  1086. if (mapping < 0) {
  1087. pr_debug("event %x:%llx not supported\n", event->attr.type,
  1088. event->attr.config);
  1089. return mapping;
  1090. }
  1091. /*
  1092. * We don't assign an index until we actually place the event onto
  1093. * hardware. Use -1 to signify that we haven't decided where to put it
  1094. * yet.
  1095. */
  1096. hwc->idx = -1;
  1097. hwc->config_base = 0;
  1098. hwc->config = 0;
  1099. hwc->event_base = 0;
  1100. /*
  1101. * Store the event encoding into the config_base field.
  1102. */
  1103. hwc->config_base |= (unsigned long)mapping;
  1104. /*
  1105. * Limit the sample_period to half of the counter width. That way, the
  1106. * new counter value is far less likely to overtake the previous one
  1107. * unless you have some serious IRQ latency issues.
  1108. */
  1109. hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
  1110. hwc->last_period = hwc->sample_period;
  1111. local64_set(&hwc->period_left, hwc->sample_period);
  1112. if (event->group_leader != event) {
  1113. if (validate_group(event) != 0)
  1114. return -EINVAL;
  1115. }
  1116. return 0;
  1117. }
  1118. static int cci_pmu_event_init(struct perf_event *event)
  1119. {
  1120. struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
  1121. atomic_t *active_events = &cci_pmu->active_events;
  1122. int err = 0;
  1123. if (event->attr.type != event->pmu->type)
  1124. return -ENOENT;
  1125. /* Shared by all CPUs, no meaningful state to sample */
  1126. if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
  1127. return -EOPNOTSUPP;
  1128. /* We have no filtering of any kind */
  1129. if (event->attr.exclude_user ||
  1130. event->attr.exclude_kernel ||
  1131. event->attr.exclude_hv ||
  1132. event->attr.exclude_idle ||
  1133. event->attr.exclude_host ||
  1134. event->attr.exclude_guest)
  1135. return -EINVAL;
  1136. /*
  1137. * Following the example set by other "uncore" PMUs, we accept any CPU
  1138. * and rewrite its affinity dynamically rather than having perf core
  1139. * handle cpu == -1 and pid == -1 for this case.
  1140. *
  1141. * The perf core will pin online CPUs for the duration of this call and
  1142. * the event being installed into its context, so the PMU's CPU can't
  1143. * change under our feet.
  1144. */
  1145. if (event->cpu < 0)
  1146. return -EINVAL;
  1147. event->cpu = cci_pmu->cpu;
  1148. event->destroy = hw_perf_event_destroy;
  1149. if (!atomic_inc_not_zero(active_events)) {
  1150. mutex_lock(&cci_pmu->reserve_mutex);
  1151. if (atomic_read(active_events) == 0)
  1152. err = cci_pmu_get_hw(cci_pmu);
  1153. if (!err)
  1154. atomic_inc(active_events);
  1155. mutex_unlock(&cci_pmu->reserve_mutex);
  1156. }
  1157. if (err)
  1158. return err;
  1159. err = __hw_perf_event_init(event);
  1160. if (err)
  1161. hw_perf_event_destroy(event);
  1162. return err;
  1163. }
  1164. static ssize_t pmu_cpumask_attr_show(struct device *dev,
  1165. struct device_attribute *attr, char *buf)
  1166. {
  1167. struct pmu *pmu = dev_get_drvdata(dev);
  1168. struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
  1169. return cpumap_print_to_pagebuf(true, buf, cpumask_of(cci_pmu->cpu));
  1170. }
  1171. static struct device_attribute pmu_cpumask_attr =
  1172. __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL);
  1173. static struct attribute *pmu_attrs[] = {
  1174. &pmu_cpumask_attr.attr,
  1175. NULL,
  1176. };
  1177. static struct attribute_group pmu_attr_group = {
  1178. .attrs = pmu_attrs,
  1179. };
  1180. static struct attribute_group pmu_format_attr_group = {
  1181. .name = "format",
  1182. .attrs = NULL, /* Filled in cci_pmu_init_attrs */
  1183. };
  1184. static struct attribute_group pmu_event_attr_group = {
  1185. .name = "events",
  1186. .attrs = NULL, /* Filled in cci_pmu_init_attrs */
  1187. };
  1188. static const struct attribute_group *pmu_attr_groups[] = {
  1189. &pmu_attr_group,
  1190. &pmu_format_attr_group,
  1191. &pmu_event_attr_group,
  1192. NULL
  1193. };
  1194. static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
  1195. {
  1196. const struct cci_pmu_model *model = cci_pmu->model;
  1197. char *name = model->name;
  1198. u32 num_cntrs;
  1199. pmu_event_attr_group.attrs = model->event_attrs;
  1200. pmu_format_attr_group.attrs = model->format_attrs;
  1201. cci_pmu->pmu = (struct pmu) {
  1202. .name = cci_pmu->model->name,
  1203. .task_ctx_nr = perf_invalid_context,
  1204. .pmu_enable = cci_pmu_enable,
  1205. .pmu_disable = cci_pmu_disable,
  1206. .event_init = cci_pmu_event_init,
  1207. .add = cci_pmu_add,
  1208. .del = cci_pmu_del,
  1209. .start = cci_pmu_start,
  1210. .stop = cci_pmu_stop,
  1211. .read = pmu_read,
  1212. .attr_groups = pmu_attr_groups,
  1213. };
  1214. cci_pmu->plat_device = pdev;
  1215. num_cntrs = pmu_get_max_counters(cci_pmu);
  1216. if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
  1217. dev_warn(&pdev->dev,
  1218. "PMU implements more counters(%d) than supported by"
  1219. " the model(%d), truncated.",
  1220. num_cntrs, cci_pmu->model->num_hw_cntrs);
  1221. num_cntrs = cci_pmu->model->num_hw_cntrs;
  1222. }
  1223. cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
  1224. return perf_pmu_register(&cci_pmu->pmu, name, -1);
  1225. }
  1226. static int cci_pmu_offline_cpu(unsigned int cpu)
  1227. {
  1228. int target;
  1229. if (!g_cci_pmu || cpu != g_cci_pmu->cpu)
  1230. return 0;
  1231. target = cpumask_any_but(cpu_online_mask, cpu);
  1232. if (target >= nr_cpu_ids)
  1233. return 0;
  1234. perf_pmu_migrate_context(&g_cci_pmu->pmu, cpu, target);
  1235. g_cci_pmu->cpu = target;
  1236. return 0;
  1237. }
  1238. static struct cci_pmu_model cci_pmu_models[] = {
  1239. #ifdef CONFIG_ARM_CCI400_PMU
  1240. [CCI400_R0] = {
  1241. .name = "CCI_400",
  1242. .fixed_hw_cntrs = 1, /* Cycle counter */
  1243. .num_hw_cntrs = 4,
  1244. .cntr_size = SZ_4K,
  1245. .format_attrs = cci400_pmu_format_attrs,
  1246. .event_attrs = cci400_r0_pmu_event_attrs,
  1247. .event_ranges = {
  1248. [CCI_IF_SLAVE] = {
  1249. CCI400_R0_SLAVE_PORT_MIN_EV,
  1250. CCI400_R0_SLAVE_PORT_MAX_EV,
  1251. },
  1252. [CCI_IF_MASTER] = {
  1253. CCI400_R0_MASTER_PORT_MIN_EV,
  1254. CCI400_R0_MASTER_PORT_MAX_EV,
  1255. },
  1256. },
  1257. .validate_hw_event = cci400_validate_hw_event,
  1258. .get_event_idx = cci400_get_event_idx,
  1259. },
  1260. [CCI400_R1] = {
  1261. .name = "CCI_400_r1",
  1262. .fixed_hw_cntrs = 1, /* Cycle counter */
  1263. .num_hw_cntrs = 4,
  1264. .cntr_size = SZ_4K,
  1265. .format_attrs = cci400_pmu_format_attrs,
  1266. .event_attrs = cci400_r1_pmu_event_attrs,
  1267. .event_ranges = {
  1268. [CCI_IF_SLAVE] = {
  1269. CCI400_R1_SLAVE_PORT_MIN_EV,
  1270. CCI400_R1_SLAVE_PORT_MAX_EV,
  1271. },
  1272. [CCI_IF_MASTER] = {
  1273. CCI400_R1_MASTER_PORT_MIN_EV,
  1274. CCI400_R1_MASTER_PORT_MAX_EV,
  1275. },
  1276. },
  1277. .validate_hw_event = cci400_validate_hw_event,
  1278. .get_event_idx = cci400_get_event_idx,
  1279. },
  1280. #endif
  1281. #ifdef CONFIG_ARM_CCI5xx_PMU
  1282. [CCI500_R0] = {
  1283. .name = "CCI_500",
  1284. .fixed_hw_cntrs = 0,
  1285. .num_hw_cntrs = 8,
  1286. .cntr_size = SZ_64K,
  1287. .format_attrs = cci5xx_pmu_format_attrs,
  1288. .event_attrs = cci5xx_pmu_event_attrs,
  1289. .event_ranges = {
  1290. [CCI_IF_SLAVE] = {
  1291. CCI5xx_SLAVE_PORT_MIN_EV,
  1292. CCI5xx_SLAVE_PORT_MAX_EV,
  1293. },
  1294. [CCI_IF_MASTER] = {
  1295. CCI5xx_MASTER_PORT_MIN_EV,
  1296. CCI5xx_MASTER_PORT_MAX_EV,
  1297. },
  1298. [CCI_IF_GLOBAL] = {
  1299. CCI5xx_GLOBAL_PORT_MIN_EV,
  1300. CCI5xx_GLOBAL_PORT_MAX_EV,
  1301. },
  1302. },
  1303. .validate_hw_event = cci500_validate_hw_event,
  1304. .write_counters = cci5xx_pmu_write_counters,
  1305. },
  1306. [CCI550_R0] = {
  1307. .name = "CCI_550",
  1308. .fixed_hw_cntrs = 0,
  1309. .num_hw_cntrs = 8,
  1310. .cntr_size = SZ_64K,
  1311. .format_attrs = cci5xx_pmu_format_attrs,
  1312. .event_attrs = cci5xx_pmu_event_attrs,
  1313. .event_ranges = {
  1314. [CCI_IF_SLAVE] = {
  1315. CCI5xx_SLAVE_PORT_MIN_EV,
  1316. CCI5xx_SLAVE_PORT_MAX_EV,
  1317. },
  1318. [CCI_IF_MASTER] = {
  1319. CCI5xx_MASTER_PORT_MIN_EV,
  1320. CCI5xx_MASTER_PORT_MAX_EV,
  1321. },
  1322. [CCI_IF_GLOBAL] = {
  1323. CCI5xx_GLOBAL_PORT_MIN_EV,
  1324. CCI5xx_GLOBAL_PORT_MAX_EV,
  1325. },
  1326. },
  1327. .validate_hw_event = cci550_validate_hw_event,
  1328. .write_counters = cci5xx_pmu_write_counters,
  1329. },
  1330. #endif
  1331. };
  1332. static const struct of_device_id arm_cci_pmu_matches[] = {
  1333. #ifdef CONFIG_ARM_CCI400_PMU
  1334. {
  1335. .compatible = "arm,cci-400-pmu",
  1336. .data = NULL,
  1337. },
  1338. {
  1339. .compatible = "arm,cci-400-pmu,r0",
  1340. .data = &cci_pmu_models[CCI400_R0],
  1341. },
  1342. {
  1343. .compatible = "arm,cci-400-pmu,r1",
  1344. .data = &cci_pmu_models[CCI400_R1],
  1345. },
  1346. #endif
  1347. #ifdef CONFIG_ARM_CCI5xx_PMU
  1348. {
  1349. .compatible = "arm,cci-500-pmu,r0",
  1350. .data = &cci_pmu_models[CCI500_R0],
  1351. },
  1352. {
  1353. .compatible = "arm,cci-550-pmu,r0",
  1354. .data = &cci_pmu_models[CCI550_R0],
  1355. },
  1356. #endif
  1357. {},
  1358. };
  1359. static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
  1360. {
  1361. int i;
  1362. for (i = 0; i < nr_irqs; i++)
  1363. if (irq == irqs[i])
  1364. return true;
  1365. return false;
  1366. }
  1367. static struct cci_pmu *cci_pmu_alloc(struct device *dev)
  1368. {
  1369. struct cci_pmu *cci_pmu;
  1370. const struct cci_pmu_model *model;
  1371. /*
  1372. * All allocations are devm_* hence we don't have to free
  1373. * them explicitly on an error, as it would end up in driver
  1374. * detach.
  1375. */
  1376. cci_pmu = devm_kzalloc(dev, sizeof(*cci_pmu), GFP_KERNEL);
  1377. if (!cci_pmu)
  1378. return ERR_PTR(-ENOMEM);
  1379. cci_pmu->ctrl_base = *(void __iomem **)dev->platform_data;
  1380. model = of_device_get_match_data(dev);
  1381. if (!model) {
  1382. dev_warn(dev,
  1383. "DEPRECATED compatible property, requires secure access to CCI registers");
  1384. model = probe_cci_model(cci_pmu);
  1385. }
  1386. if (!model) {
  1387. dev_warn(dev, "CCI PMU version not supported\n");
  1388. return ERR_PTR(-ENODEV);
  1389. }
  1390. cci_pmu->model = model;
  1391. cci_pmu->irqs = devm_kcalloc(dev, CCI_PMU_MAX_HW_CNTRS(model),
  1392. sizeof(*cci_pmu->irqs), GFP_KERNEL);
  1393. if (!cci_pmu->irqs)
  1394. return ERR_PTR(-ENOMEM);
  1395. cci_pmu->hw_events.events = devm_kcalloc(dev,
  1396. CCI_PMU_MAX_HW_CNTRS(model),
  1397. sizeof(*cci_pmu->hw_events.events),
  1398. GFP_KERNEL);
  1399. if (!cci_pmu->hw_events.events)
  1400. return ERR_PTR(-ENOMEM);
  1401. cci_pmu->hw_events.used_mask = devm_kcalloc(dev,
  1402. BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
  1403. sizeof(*cci_pmu->hw_events.used_mask),
  1404. GFP_KERNEL);
  1405. if (!cci_pmu->hw_events.used_mask)
  1406. return ERR_PTR(-ENOMEM);
  1407. return cci_pmu;
  1408. }
  1409. static int cci_pmu_probe(struct platform_device *pdev)
  1410. {
  1411. struct resource *res;
  1412. struct cci_pmu *cci_pmu;
  1413. int i, ret, irq;
  1414. cci_pmu = cci_pmu_alloc(&pdev->dev);
  1415. if (IS_ERR(cci_pmu))
  1416. return PTR_ERR(cci_pmu);
  1417. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1418. cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
  1419. if (IS_ERR(cci_pmu->base))
  1420. return -ENOMEM;
  1421. /*
  1422. * CCI PMU has one overflow interrupt per counter; but some may be tied
  1423. * together to a common interrupt.
  1424. */
  1425. cci_pmu->nr_irqs = 0;
  1426. for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
  1427. irq = platform_get_irq(pdev, i);
  1428. if (irq < 0)
  1429. break;
  1430. if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
  1431. continue;
  1432. cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
  1433. }
  1434. /*
  1435. * Ensure that the device tree has as many interrupts as the number
  1436. * of counters.
  1437. */
  1438. if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
  1439. dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
  1440. i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
  1441. return -EINVAL;
  1442. }
  1443. raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
  1444. mutex_init(&cci_pmu->reserve_mutex);
  1445. atomic_set(&cci_pmu->active_events, 0);
  1446. cci_pmu->cpu = get_cpu();
  1447. ret = cci_pmu_init(cci_pmu, pdev);
  1448. if (ret) {
  1449. put_cpu();
  1450. return ret;
  1451. }
  1452. cpuhp_setup_state_nocalls(CPUHP_AP_PERF_ARM_CCI_ONLINE,
  1453. "perf/arm/cci:online", NULL,
  1454. cci_pmu_offline_cpu);
  1455. put_cpu();
  1456. g_cci_pmu = cci_pmu;
  1457. pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
  1458. return 0;
  1459. }
  1460. static struct platform_driver cci_pmu_driver = {
  1461. .driver = {
  1462. .name = DRIVER_NAME,
  1463. .of_match_table = arm_cci_pmu_matches,
  1464. },
  1465. .probe = cci_pmu_probe,
  1466. };
  1467. builtin_platform_driver(cci_pmu_driver);
  1468. MODULE_LICENSE("GPL");
  1469. MODULE_DESCRIPTION("ARM CCI PMU support");