arm-cci.c 47 KB

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