arm-ccn.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright (C) 2014 ARM Limited
  12. */
  13. #include <linux/ctype.h>
  14. #include <linux/hrtimer.h>
  15. #include <linux/idr.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/perf_event.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #define CCN_NUM_XP_PORTS 2
  24. #define CCN_NUM_VCS 4
  25. #define CCN_NUM_REGIONS 256
  26. #define CCN_REGION_SIZE 0x10000
  27. #define CCN_ALL_OLY_ID 0xff00
  28. #define CCN_ALL_OLY_ID__OLY_ID__SHIFT 0
  29. #define CCN_ALL_OLY_ID__OLY_ID__MASK 0x1f
  30. #define CCN_ALL_OLY_ID__NODE_ID__SHIFT 8
  31. #define CCN_ALL_OLY_ID__NODE_ID__MASK 0x3f
  32. #define CCN_MN_ERRINT_STATUS 0x0008
  33. #define CCN_MN_ERRINT_STATUS__INTREQ__DESSERT 0x11
  34. #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__ENABLE 0x02
  35. #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLED 0x20
  36. #define CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE 0x22
  37. #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_ENABLE 0x04
  38. #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLED 0x40
  39. #define CCN_MN_ERRINT_STATUS__CORRECTED_ERRORS_DISABLE 0x44
  40. #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE 0x08
  41. #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED 0x80
  42. #define CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE 0x88
  43. #define CCN_MN_OLY_COMP_LIST_63_0 0x01e0
  44. #define CCN_MN_ERR_SIG_VAL_63_0 0x0300
  45. #define CCN_MN_ERR_SIG_VAL_63_0__DT (1 << 1)
  46. #define CCN_DT_ACTIVE_DSM 0x0000
  47. #define CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(n) ((n) * 8)
  48. #define CCN_DT_ACTIVE_DSM__DSM_ID__MASK 0xff
  49. #define CCN_DT_CTL 0x0028
  50. #define CCN_DT_CTL__DT_EN (1 << 0)
  51. #define CCN_DT_PMEVCNT(n) (0x0100 + (n) * 0x8)
  52. #define CCN_DT_PMCCNTR 0x0140
  53. #define CCN_DT_PMCCNTRSR 0x0190
  54. #define CCN_DT_PMOVSR 0x0198
  55. #define CCN_DT_PMOVSR_CLR 0x01a0
  56. #define CCN_DT_PMOVSR_CLR__MASK 0x1f
  57. #define CCN_DT_PMCR 0x01a8
  58. #define CCN_DT_PMCR__OVFL_INTR_EN (1 << 6)
  59. #define CCN_DT_PMCR__PMU_EN (1 << 0)
  60. #define CCN_DT_PMSR 0x01b0
  61. #define CCN_DT_PMSR_REQ 0x01b8
  62. #define CCN_DT_PMSR_CLR 0x01c0
  63. #define CCN_HNF_PMU_EVENT_SEL 0x0600
  64. #define CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
  65. #define CCN_HNF_PMU_EVENT_SEL__ID__MASK 0xf
  66. #define CCN_XP_DT_CONFIG 0x0300
  67. #define CCN_XP_DT_CONFIG__DT_CFG__SHIFT(n) ((n) * 4)
  68. #define CCN_XP_DT_CONFIG__DT_CFG__MASK 0xf
  69. #define CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH 0x0
  70. #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT_0_OR_1 0x1
  71. #define CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(n) (0x2 + (n))
  72. #define CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(n) (0x4 + (n))
  73. #define CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(d, n) (0x8 + (d) * 4 + (n))
  74. #define CCN_XP_DT_INTERFACE_SEL 0x0308
  75. #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(n) (0 + (n) * 8)
  76. #define CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK 0x1
  77. #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(n) (1 + (n) * 8)
  78. #define CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK 0x1
  79. #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(n) (2 + (n) * 8)
  80. #define CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK 0x3
  81. #define CCN_XP_DT_CMP_VAL_L(n) (0x0310 + (n) * 0x40)
  82. #define CCN_XP_DT_CMP_VAL_H(n) (0x0318 + (n) * 0x40)
  83. #define CCN_XP_DT_CMP_MASK_L(n) (0x0320 + (n) * 0x40)
  84. #define CCN_XP_DT_CMP_MASK_H(n) (0x0328 + (n) * 0x40)
  85. #define CCN_XP_DT_CONTROL 0x0370
  86. #define CCN_XP_DT_CONTROL__DT_ENABLE (1 << 0)
  87. #define CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(n) (12 + (n) * 4)
  88. #define CCN_XP_DT_CONTROL__WP_ARM_SEL__MASK 0xf
  89. #define CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS 0xf
  90. #define CCN_XP_PMU_EVENT_SEL 0x0600
  91. #define CCN_XP_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 7)
  92. #define CCN_XP_PMU_EVENT_SEL__ID__MASK 0x3f
  93. #define CCN_SBAS_PMU_EVENT_SEL 0x0600
  94. #define CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
  95. #define CCN_SBAS_PMU_EVENT_SEL__ID__MASK 0xf
  96. #define CCN_RNI_PMU_EVENT_SEL 0x0600
  97. #define CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(n) ((n) * 4)
  98. #define CCN_RNI_PMU_EVENT_SEL__ID__MASK 0xf
  99. #define CCN_TYPE_MN 0x01
  100. #define CCN_TYPE_DT 0x02
  101. #define CCN_TYPE_HNF 0x04
  102. #define CCN_TYPE_HNI 0x05
  103. #define CCN_TYPE_XP 0x08
  104. #define CCN_TYPE_SBSX 0x0c
  105. #define CCN_TYPE_SBAS 0x10
  106. #define CCN_TYPE_RNI_1P 0x14
  107. #define CCN_TYPE_RNI_2P 0x15
  108. #define CCN_TYPE_RNI_3P 0x16
  109. #define CCN_TYPE_RND_1P 0x18 /* RN-D = RN-I + DVM */
  110. #define CCN_TYPE_RND_2P 0x19
  111. #define CCN_TYPE_RND_3P 0x1a
  112. #define CCN_TYPE_CYCLES 0xff /* Pseudotype */
  113. #define CCN_EVENT_WATCHPOINT 0xfe /* Pseudoevent */
  114. #define CCN_NUM_PMU_EVENTS 4
  115. #define CCN_NUM_XP_WATCHPOINTS 2 /* See DT.dbg_id.num_watchpoints */
  116. #define CCN_NUM_PMU_EVENT_COUNTERS 8 /* See DT.dbg_id.num_pmucntr */
  117. #define CCN_IDX_PMU_CYCLE_COUNTER CCN_NUM_PMU_EVENT_COUNTERS
  118. #define CCN_NUM_PREDEFINED_MASKS 4
  119. #define CCN_IDX_MASK_ANY (CCN_NUM_PMU_EVENT_COUNTERS + 0)
  120. #define CCN_IDX_MASK_EXACT (CCN_NUM_PMU_EVENT_COUNTERS + 1)
  121. #define CCN_IDX_MASK_ORDER (CCN_NUM_PMU_EVENT_COUNTERS + 2)
  122. #define CCN_IDX_MASK_OPCODE (CCN_NUM_PMU_EVENT_COUNTERS + 3)
  123. struct arm_ccn_component {
  124. void __iomem *base;
  125. u32 type;
  126. DECLARE_BITMAP(pmu_events_mask, CCN_NUM_PMU_EVENTS);
  127. union {
  128. struct {
  129. DECLARE_BITMAP(dt_cmp_mask, CCN_NUM_XP_WATCHPOINTS);
  130. } xp;
  131. };
  132. };
  133. #define pmu_to_arm_ccn(_pmu) container_of(container_of(_pmu, \
  134. struct arm_ccn_dt, pmu), struct arm_ccn, dt)
  135. struct arm_ccn_dt {
  136. int id;
  137. void __iomem *base;
  138. spinlock_t config_lock;
  139. DECLARE_BITMAP(pmu_counters_mask, CCN_NUM_PMU_EVENT_COUNTERS + 1);
  140. struct {
  141. struct arm_ccn_component *source;
  142. struct perf_event *event;
  143. } pmu_counters[CCN_NUM_PMU_EVENT_COUNTERS + 1];
  144. struct {
  145. u64 l, h;
  146. } cmp_mask[CCN_NUM_PMU_EVENT_COUNTERS + CCN_NUM_PREDEFINED_MASKS];
  147. struct hrtimer hrtimer;
  148. cpumask_t cpu;
  149. struct hlist_node node;
  150. struct pmu pmu;
  151. };
  152. struct arm_ccn {
  153. struct device *dev;
  154. void __iomem *base;
  155. unsigned int irq;
  156. unsigned sbas_present:1;
  157. unsigned sbsx_present:1;
  158. int num_nodes;
  159. struct arm_ccn_component *node;
  160. int num_xps;
  161. struct arm_ccn_component *xp;
  162. struct arm_ccn_dt dt;
  163. int mn_id;
  164. };
  165. static int arm_ccn_node_to_xp(int node)
  166. {
  167. return node / CCN_NUM_XP_PORTS;
  168. }
  169. static int arm_ccn_node_to_xp_port(int node)
  170. {
  171. return node % CCN_NUM_XP_PORTS;
  172. }
  173. /*
  174. * Bit shifts and masks in these defines must be kept in sync with
  175. * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
  176. */
  177. #define CCN_CONFIG_NODE(_config) (((_config) >> 0) & 0xff)
  178. #define CCN_CONFIG_XP(_config) (((_config) >> 0) & 0xff)
  179. #define CCN_CONFIG_TYPE(_config) (((_config) >> 8) & 0xff)
  180. #define CCN_CONFIG_EVENT(_config) (((_config) >> 16) & 0xff)
  181. #define CCN_CONFIG_PORT(_config) (((_config) >> 24) & 0x3)
  182. #define CCN_CONFIG_BUS(_config) (((_config) >> 24) & 0x3)
  183. #define CCN_CONFIG_VC(_config) (((_config) >> 26) & 0x7)
  184. #define CCN_CONFIG_DIR(_config) (((_config) >> 29) & 0x1)
  185. #define CCN_CONFIG_MASK(_config) (((_config) >> 30) & 0xf)
  186. static void arm_ccn_pmu_config_set(u64 *config, u32 node_xp, u32 type, u32 port)
  187. {
  188. *config &= ~((0xff << 0) | (0xff << 8) | (0x3 << 24));
  189. *config |= (node_xp << 0) | (type << 8) | (port << 24);
  190. }
  191. static ssize_t arm_ccn_pmu_format_show(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct dev_ext_attribute *ea = container_of(attr,
  195. struct dev_ext_attribute, attr);
  196. return snprintf(buf, PAGE_SIZE, "%s\n", (char *)ea->var);
  197. }
  198. #define CCN_FORMAT_ATTR(_name, _config) \
  199. struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
  200. { __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
  201. NULL), _config }
  202. static CCN_FORMAT_ATTR(node, "config:0-7");
  203. static CCN_FORMAT_ATTR(xp, "config:0-7");
  204. static CCN_FORMAT_ATTR(type, "config:8-15");
  205. static CCN_FORMAT_ATTR(event, "config:16-23");
  206. static CCN_FORMAT_ATTR(port, "config:24-25");
  207. static CCN_FORMAT_ATTR(bus, "config:24-25");
  208. static CCN_FORMAT_ATTR(vc, "config:26-28");
  209. static CCN_FORMAT_ATTR(dir, "config:29-29");
  210. static CCN_FORMAT_ATTR(mask, "config:30-33");
  211. static CCN_FORMAT_ATTR(cmp_l, "config1:0-62");
  212. static CCN_FORMAT_ATTR(cmp_h, "config2:0-59");
  213. static struct attribute *arm_ccn_pmu_format_attrs[] = {
  214. &arm_ccn_pmu_format_attr_node.attr.attr,
  215. &arm_ccn_pmu_format_attr_xp.attr.attr,
  216. &arm_ccn_pmu_format_attr_type.attr.attr,
  217. &arm_ccn_pmu_format_attr_event.attr.attr,
  218. &arm_ccn_pmu_format_attr_port.attr.attr,
  219. &arm_ccn_pmu_format_attr_bus.attr.attr,
  220. &arm_ccn_pmu_format_attr_vc.attr.attr,
  221. &arm_ccn_pmu_format_attr_dir.attr.attr,
  222. &arm_ccn_pmu_format_attr_mask.attr.attr,
  223. &arm_ccn_pmu_format_attr_cmp_l.attr.attr,
  224. &arm_ccn_pmu_format_attr_cmp_h.attr.attr,
  225. NULL
  226. };
  227. static const struct attribute_group arm_ccn_pmu_format_attr_group = {
  228. .name = "format",
  229. .attrs = arm_ccn_pmu_format_attrs,
  230. };
  231. struct arm_ccn_pmu_event {
  232. struct device_attribute attr;
  233. u32 type;
  234. u32 event;
  235. int num_ports;
  236. int num_vcs;
  237. const char *def;
  238. int mask;
  239. };
  240. #define CCN_EVENT_ATTR(_name) \
  241. __ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
  242. /*
  243. * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
  244. * their ports in XP they are connected to. For the sake of usability they are
  245. * explicitly defined here (and translated into a relevant watchpoint in
  246. * arm_ccn_pmu_event_init()) so the user can easily request them without deep
  247. * knowledge of the flit format.
  248. */
  249. #define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
  250. .type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
  251. .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
  252. .def = _def, .mask = _mask, }
  253. #define CCN_EVENT_HNI(_name, _def, _mask) { \
  254. .attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
  255. .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
  256. .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
  257. #define CCN_EVENT_SBSX(_name, _def, _mask) { \
  258. .attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
  259. .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
  260. .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
  261. #define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
  262. .type = CCN_TYPE_HNF, .event = _event, }
  263. #define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
  264. .type = CCN_TYPE_XP, .event = _event, \
  265. .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
  266. /*
  267. * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
  268. * on configuration. One of them is picked to represent the whole group,
  269. * as they all share the same event types.
  270. */
  271. #define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
  272. .type = CCN_TYPE_RNI_3P, .event = _event, }
  273. #define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
  274. .type = CCN_TYPE_SBAS, .event = _event, }
  275. #define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
  276. .type = CCN_TYPE_CYCLES }
  277. static ssize_t arm_ccn_pmu_event_show(struct device *dev,
  278. struct device_attribute *attr, char *buf)
  279. {
  280. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  281. struct arm_ccn_pmu_event *event = container_of(attr,
  282. struct arm_ccn_pmu_event, attr);
  283. ssize_t res;
  284. res = snprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
  285. if (event->event)
  286. res += snprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
  287. event->event);
  288. if (event->def)
  289. res += snprintf(buf + res, PAGE_SIZE - res, ",%s",
  290. event->def);
  291. if (event->mask)
  292. res += snprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
  293. event->mask);
  294. /* Arguments required by an event */
  295. switch (event->type) {
  296. case CCN_TYPE_CYCLES:
  297. break;
  298. case CCN_TYPE_XP:
  299. res += snprintf(buf + res, PAGE_SIZE - res,
  300. ",xp=?,vc=?");
  301. if (event->event == CCN_EVENT_WATCHPOINT)
  302. res += snprintf(buf + res, PAGE_SIZE - res,
  303. ",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
  304. else
  305. res += snprintf(buf + res, PAGE_SIZE - res,
  306. ",bus=?");
  307. break;
  308. case CCN_TYPE_MN:
  309. res += snprintf(buf + res, PAGE_SIZE - res, ",node=%d", ccn->mn_id);
  310. break;
  311. default:
  312. res += snprintf(buf + res, PAGE_SIZE - res, ",node=?");
  313. break;
  314. }
  315. res += snprintf(buf + res, PAGE_SIZE - res, "\n");
  316. return res;
  317. }
  318. static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
  319. struct attribute *attr, int index)
  320. {
  321. struct device *dev = kobj_to_dev(kobj);
  322. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  323. struct device_attribute *dev_attr = container_of(attr,
  324. struct device_attribute, attr);
  325. struct arm_ccn_pmu_event *event = container_of(dev_attr,
  326. struct arm_ccn_pmu_event, attr);
  327. if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
  328. return 0;
  329. if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
  330. return 0;
  331. return attr->mode;
  332. }
  333. static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
  334. CCN_EVENT_MN(eobarrier, "dir=1,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
  335. CCN_EVENT_MN(ecbarrier, "dir=1,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
  336. CCN_EVENT_MN(dvmop, "dir=1,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
  337. CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
  338. CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
  339. CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
  340. CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
  341. CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
  342. CCN_IDX_MASK_ORDER),
  343. CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
  344. CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
  345. CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
  346. CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
  347. CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
  348. CCN_IDX_MASK_ORDER),
  349. CCN_EVENT_HNF(cache_miss, 0x1),
  350. CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
  351. CCN_EVENT_HNF(cache_fill, 0x3),
  352. CCN_EVENT_HNF(pocq_retry, 0x4),
  353. CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
  354. CCN_EVENT_HNF(sf_hit, 0x6),
  355. CCN_EVENT_HNF(sf_evictions, 0x7),
  356. CCN_EVENT_HNF(snoops_sent, 0x8),
  357. CCN_EVENT_HNF(snoops_broadcast, 0x9),
  358. CCN_EVENT_HNF(l3_eviction, 0xa),
  359. CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
  360. CCN_EVENT_HNF(mc_retries, 0xc),
  361. CCN_EVENT_HNF(mc_reqs, 0xd),
  362. CCN_EVENT_HNF(qos_hh_retry, 0xe),
  363. CCN_EVENT_RNI(rdata_beats_p0, 0x1),
  364. CCN_EVENT_RNI(rdata_beats_p1, 0x2),
  365. CCN_EVENT_RNI(rdata_beats_p2, 0x3),
  366. CCN_EVENT_RNI(rxdat_flits, 0x4),
  367. CCN_EVENT_RNI(txdat_flits, 0x5),
  368. CCN_EVENT_RNI(txreq_flits, 0x6),
  369. CCN_EVENT_RNI(txreq_flits_retried, 0x7),
  370. CCN_EVENT_RNI(rrt_full, 0x8),
  371. CCN_EVENT_RNI(wrt_full, 0x9),
  372. CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
  373. CCN_EVENT_XP(upload_starvation, 0x1),
  374. CCN_EVENT_XP(download_starvation, 0x2),
  375. CCN_EVENT_XP(respin, 0x3),
  376. CCN_EVENT_XP(valid_flit, 0x4),
  377. CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
  378. CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
  379. CCN_EVENT_SBAS(rxdat_flits, 0x4),
  380. CCN_EVENT_SBAS(txdat_flits, 0x5),
  381. CCN_EVENT_SBAS(txreq_flits, 0x6),
  382. CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
  383. CCN_EVENT_SBAS(rrt_full, 0x8),
  384. CCN_EVENT_SBAS(wrt_full, 0x9),
  385. CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
  386. CCN_EVENT_CYCLES(cycles),
  387. };
  388. /* Populated in arm_ccn_init() */
  389. static struct attribute
  390. *arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
  391. static const struct attribute_group arm_ccn_pmu_events_attr_group = {
  392. .name = "events",
  393. .is_visible = arm_ccn_pmu_events_is_visible,
  394. .attrs = arm_ccn_pmu_events_attrs,
  395. };
  396. static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
  397. {
  398. unsigned long i;
  399. if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
  400. return NULL;
  401. i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
  402. switch (name[1]) {
  403. case 'l':
  404. return &ccn->dt.cmp_mask[i].l;
  405. case 'h':
  406. return &ccn->dt.cmp_mask[i].h;
  407. default:
  408. return NULL;
  409. }
  410. }
  411. static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
  412. struct device_attribute *attr, char *buf)
  413. {
  414. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  415. u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
  416. return mask ? snprintf(buf, PAGE_SIZE, "0x%016llx\n", *mask) : -EINVAL;
  417. }
  418. static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
  419. struct device_attribute *attr, const char *buf, size_t count)
  420. {
  421. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  422. u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
  423. int err = -EINVAL;
  424. if (mask)
  425. err = kstrtoull(buf, 0, mask);
  426. return err ? err : count;
  427. }
  428. #define CCN_CMP_MASK_ATTR(_name) \
  429. struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
  430. __ATTR(_name, S_IRUGO | S_IWUSR, \
  431. arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
  432. #define CCN_CMP_MASK_ATTR_RO(_name) \
  433. struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
  434. __ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
  435. static CCN_CMP_MASK_ATTR(0l);
  436. static CCN_CMP_MASK_ATTR(0h);
  437. static CCN_CMP_MASK_ATTR(1l);
  438. static CCN_CMP_MASK_ATTR(1h);
  439. static CCN_CMP_MASK_ATTR(2l);
  440. static CCN_CMP_MASK_ATTR(2h);
  441. static CCN_CMP_MASK_ATTR(3l);
  442. static CCN_CMP_MASK_ATTR(3h);
  443. static CCN_CMP_MASK_ATTR(4l);
  444. static CCN_CMP_MASK_ATTR(4h);
  445. static CCN_CMP_MASK_ATTR(5l);
  446. static CCN_CMP_MASK_ATTR(5h);
  447. static CCN_CMP_MASK_ATTR(6l);
  448. static CCN_CMP_MASK_ATTR(6h);
  449. static CCN_CMP_MASK_ATTR(7l);
  450. static CCN_CMP_MASK_ATTR(7h);
  451. static CCN_CMP_MASK_ATTR_RO(8l);
  452. static CCN_CMP_MASK_ATTR_RO(8h);
  453. static CCN_CMP_MASK_ATTR_RO(9l);
  454. static CCN_CMP_MASK_ATTR_RO(9h);
  455. static CCN_CMP_MASK_ATTR_RO(al);
  456. static CCN_CMP_MASK_ATTR_RO(ah);
  457. static CCN_CMP_MASK_ATTR_RO(bl);
  458. static CCN_CMP_MASK_ATTR_RO(bh);
  459. static struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
  460. &arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
  461. &arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
  462. &arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
  463. &arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
  464. &arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
  465. &arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
  466. &arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
  467. &arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
  468. &arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
  469. &arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
  470. &arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
  471. &arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
  472. NULL
  473. };
  474. static const struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
  475. .name = "cmp_mask",
  476. .attrs = arm_ccn_pmu_cmp_mask_attrs,
  477. };
  478. static ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
  479. struct device_attribute *attr, char *buf)
  480. {
  481. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  482. return cpumap_print_to_pagebuf(true, buf, &ccn->dt.cpu);
  483. }
  484. static struct device_attribute arm_ccn_pmu_cpumask_attr =
  485. __ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
  486. static struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
  487. &arm_ccn_pmu_cpumask_attr.attr,
  488. NULL,
  489. };
  490. static const struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
  491. .attrs = arm_ccn_pmu_cpumask_attrs,
  492. };
  493. /*
  494. * Default poll period is 10ms, which is way over the top anyway,
  495. * as in the worst case scenario (an event every cycle), with 1GHz
  496. * clocked bus, the smallest, 32 bit counter will overflow in
  497. * more than 4s.
  498. */
  499. static unsigned int arm_ccn_pmu_poll_period_us = 10000;
  500. module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
  501. S_IRUGO | S_IWUSR);
  502. static ktime_t arm_ccn_pmu_timer_period(void)
  503. {
  504. return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
  505. }
  506. static const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
  507. &arm_ccn_pmu_events_attr_group,
  508. &arm_ccn_pmu_format_attr_group,
  509. &arm_ccn_pmu_cmp_mask_attr_group,
  510. &arm_ccn_pmu_cpumask_attr_group,
  511. NULL
  512. };
  513. static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
  514. {
  515. int bit;
  516. do {
  517. bit = find_first_zero_bit(bitmap, size);
  518. if (bit >= size)
  519. return -EAGAIN;
  520. } while (test_and_set_bit(bit, bitmap));
  521. return bit;
  522. }
  523. /* All RN-I and RN-D nodes have identical PMUs */
  524. static int arm_ccn_pmu_type_eq(u32 a, u32 b)
  525. {
  526. if (a == b)
  527. return 1;
  528. switch (a) {
  529. case CCN_TYPE_RNI_1P:
  530. case CCN_TYPE_RNI_2P:
  531. case CCN_TYPE_RNI_3P:
  532. case CCN_TYPE_RND_1P:
  533. case CCN_TYPE_RND_2P:
  534. case CCN_TYPE_RND_3P:
  535. switch (b) {
  536. case CCN_TYPE_RNI_1P:
  537. case CCN_TYPE_RNI_2P:
  538. case CCN_TYPE_RNI_3P:
  539. case CCN_TYPE_RND_1P:
  540. case CCN_TYPE_RND_2P:
  541. case CCN_TYPE_RND_3P:
  542. return 1;
  543. }
  544. break;
  545. }
  546. return 0;
  547. }
  548. static int arm_ccn_pmu_event_alloc(struct perf_event *event)
  549. {
  550. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  551. struct hw_perf_event *hw = &event->hw;
  552. u32 node_xp, type, event_id;
  553. struct arm_ccn_component *source;
  554. int bit;
  555. node_xp = CCN_CONFIG_NODE(event->attr.config);
  556. type = CCN_CONFIG_TYPE(event->attr.config);
  557. event_id = CCN_CONFIG_EVENT(event->attr.config);
  558. /* Allocate the cycle counter */
  559. if (type == CCN_TYPE_CYCLES) {
  560. if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
  561. ccn->dt.pmu_counters_mask))
  562. return -EAGAIN;
  563. hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
  564. ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
  565. return 0;
  566. }
  567. /* Allocate an event counter */
  568. hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
  569. CCN_NUM_PMU_EVENT_COUNTERS);
  570. if (hw->idx < 0) {
  571. dev_dbg(ccn->dev, "No more counters available!\n");
  572. return -EAGAIN;
  573. }
  574. if (type == CCN_TYPE_XP)
  575. source = &ccn->xp[node_xp];
  576. else
  577. source = &ccn->node[node_xp];
  578. ccn->dt.pmu_counters[hw->idx].source = source;
  579. /* Allocate an event source or a watchpoint */
  580. if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
  581. bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
  582. CCN_NUM_XP_WATCHPOINTS);
  583. else
  584. bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
  585. CCN_NUM_PMU_EVENTS);
  586. if (bit < 0) {
  587. dev_dbg(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
  588. node_xp);
  589. clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
  590. return -EAGAIN;
  591. }
  592. hw->config_base = bit;
  593. ccn->dt.pmu_counters[hw->idx].event = event;
  594. return 0;
  595. }
  596. static void arm_ccn_pmu_event_release(struct perf_event *event)
  597. {
  598. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  599. struct hw_perf_event *hw = &event->hw;
  600. if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
  601. clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
  602. } else {
  603. struct arm_ccn_component *source =
  604. ccn->dt.pmu_counters[hw->idx].source;
  605. if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
  606. CCN_CONFIG_EVENT(event->attr.config) ==
  607. CCN_EVENT_WATCHPOINT)
  608. clear_bit(hw->config_base, source->xp.dt_cmp_mask);
  609. else
  610. clear_bit(hw->config_base, source->pmu_events_mask);
  611. clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
  612. }
  613. ccn->dt.pmu_counters[hw->idx].source = NULL;
  614. ccn->dt.pmu_counters[hw->idx].event = NULL;
  615. }
  616. static int arm_ccn_pmu_event_init(struct perf_event *event)
  617. {
  618. struct arm_ccn *ccn;
  619. struct hw_perf_event *hw = &event->hw;
  620. u32 node_xp, type, event_id;
  621. int valid;
  622. int i;
  623. struct perf_event *sibling;
  624. if (event->attr.type != event->pmu->type)
  625. return -ENOENT;
  626. ccn = pmu_to_arm_ccn(event->pmu);
  627. if (hw->sample_period) {
  628. dev_dbg(ccn->dev, "Sampling not supported!\n");
  629. return -EOPNOTSUPP;
  630. }
  631. if (has_branch_stack(event) || event->attr.exclude_user ||
  632. event->attr.exclude_kernel || event->attr.exclude_hv ||
  633. event->attr.exclude_idle || event->attr.exclude_host ||
  634. event->attr.exclude_guest) {
  635. dev_dbg(ccn->dev, "Can't exclude execution levels!\n");
  636. return -EINVAL;
  637. }
  638. if (event->cpu < 0) {
  639. dev_dbg(ccn->dev, "Can't provide per-task data!\n");
  640. return -EOPNOTSUPP;
  641. }
  642. /*
  643. * Many perf core operations (eg. events rotation) operate on a
  644. * single CPU context. This is obvious for CPU PMUs, where one
  645. * expects the same sets of events being observed on all CPUs,
  646. * but can lead to issues for off-core PMUs, like CCN, where each
  647. * event could be theoretically assigned to a different CPU. To
  648. * mitigate this, we enforce CPU assignment to one, selected
  649. * processor (the one described in the "cpumask" attribute).
  650. */
  651. event->cpu = cpumask_first(&ccn->dt.cpu);
  652. node_xp = CCN_CONFIG_NODE(event->attr.config);
  653. type = CCN_CONFIG_TYPE(event->attr.config);
  654. event_id = CCN_CONFIG_EVENT(event->attr.config);
  655. /* Validate node/xp vs topology */
  656. switch (type) {
  657. case CCN_TYPE_MN:
  658. if (node_xp != ccn->mn_id) {
  659. dev_dbg(ccn->dev, "Invalid MN ID %d!\n", node_xp);
  660. return -EINVAL;
  661. }
  662. break;
  663. case CCN_TYPE_XP:
  664. if (node_xp >= ccn->num_xps) {
  665. dev_dbg(ccn->dev, "Invalid XP ID %d!\n", node_xp);
  666. return -EINVAL;
  667. }
  668. break;
  669. case CCN_TYPE_CYCLES:
  670. break;
  671. default:
  672. if (node_xp >= ccn->num_nodes) {
  673. dev_dbg(ccn->dev, "Invalid node ID %d!\n", node_xp);
  674. return -EINVAL;
  675. }
  676. if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
  677. dev_dbg(ccn->dev, "Invalid type 0x%x for node %d!\n",
  678. type, node_xp);
  679. return -EINVAL;
  680. }
  681. break;
  682. }
  683. /* Validate event ID vs available for the type */
  684. for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
  685. i++) {
  686. struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
  687. u32 port = CCN_CONFIG_PORT(event->attr.config);
  688. u32 vc = CCN_CONFIG_VC(event->attr.config);
  689. if (!arm_ccn_pmu_type_eq(type, e->type))
  690. continue;
  691. if (event_id != e->event)
  692. continue;
  693. if (e->num_ports && port >= e->num_ports) {
  694. dev_dbg(ccn->dev, "Invalid port %d for node/XP %d!\n",
  695. port, node_xp);
  696. return -EINVAL;
  697. }
  698. if (e->num_vcs && vc >= e->num_vcs) {
  699. dev_dbg(ccn->dev, "Invalid vc %d for node/XP %d!\n",
  700. vc, node_xp);
  701. return -EINVAL;
  702. }
  703. valid = 1;
  704. }
  705. if (!valid) {
  706. dev_dbg(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
  707. event_id, node_xp);
  708. return -EINVAL;
  709. }
  710. /* Watchpoint-based event for a node is actually set on XP */
  711. if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
  712. u32 port;
  713. type = CCN_TYPE_XP;
  714. port = arm_ccn_node_to_xp_port(node_xp);
  715. node_xp = arm_ccn_node_to_xp(node_xp);
  716. arm_ccn_pmu_config_set(&event->attr.config,
  717. node_xp, type, port);
  718. }
  719. /*
  720. * We must NOT create groups containing mixed PMUs, although software
  721. * events are acceptable (for example to create a CCN group
  722. * periodically read when a hrtimer aka cpu-clock leader triggers).
  723. */
  724. if (event->group_leader->pmu != event->pmu &&
  725. !is_software_event(event->group_leader))
  726. return -EINVAL;
  727. for_each_sibling_event(sibling, event->group_leader) {
  728. if (sibling->pmu != event->pmu &&
  729. !is_software_event(sibling))
  730. return -EINVAL;
  731. }
  732. return 0;
  733. }
  734. static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
  735. {
  736. u64 res;
  737. if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
  738. #ifdef readq
  739. res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
  740. #else
  741. /* 40 bit counter, can do snapshot and read in two parts */
  742. writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
  743. while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
  744. ;
  745. writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
  746. res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
  747. res <<= 32;
  748. res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
  749. #endif
  750. } else {
  751. res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
  752. }
  753. return res;
  754. }
  755. static void arm_ccn_pmu_event_update(struct perf_event *event)
  756. {
  757. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  758. struct hw_perf_event *hw = &event->hw;
  759. u64 prev_count, new_count, mask;
  760. do {
  761. prev_count = local64_read(&hw->prev_count);
  762. new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
  763. } while (local64_xchg(&hw->prev_count, new_count) != prev_count);
  764. mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
  765. local64_add((new_count - prev_count) & mask, &event->count);
  766. }
  767. static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
  768. {
  769. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  770. struct hw_perf_event *hw = &event->hw;
  771. struct arm_ccn_component *xp;
  772. u32 val, dt_cfg;
  773. /* Nothing to do for cycle counter */
  774. if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
  775. return;
  776. if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
  777. xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
  778. else
  779. xp = &ccn->xp[arm_ccn_node_to_xp(
  780. CCN_CONFIG_NODE(event->attr.config))];
  781. if (enable)
  782. dt_cfg = hw->event_base;
  783. else
  784. dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
  785. spin_lock(&ccn->dt.config_lock);
  786. val = readl(xp->base + CCN_XP_DT_CONFIG);
  787. val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
  788. CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
  789. val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
  790. writel(val, xp->base + CCN_XP_DT_CONFIG);
  791. spin_unlock(&ccn->dt.config_lock);
  792. }
  793. static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
  794. {
  795. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  796. struct hw_perf_event *hw = &event->hw;
  797. local64_set(&event->hw.prev_count,
  798. arm_ccn_pmu_read_counter(ccn, hw->idx));
  799. hw->state = 0;
  800. /* Set the DT bus input, engaging the counter */
  801. arm_ccn_pmu_xp_dt_config(event, 1);
  802. }
  803. static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
  804. {
  805. struct hw_perf_event *hw = &event->hw;
  806. /* Disable counting, setting the DT bus to pass-through mode */
  807. arm_ccn_pmu_xp_dt_config(event, 0);
  808. if (flags & PERF_EF_UPDATE)
  809. arm_ccn_pmu_event_update(event);
  810. hw->state |= PERF_HES_STOPPED;
  811. }
  812. static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
  813. {
  814. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  815. struct hw_perf_event *hw = &event->hw;
  816. struct arm_ccn_component *source =
  817. ccn->dt.pmu_counters[hw->idx].source;
  818. unsigned long wp = hw->config_base;
  819. u32 val;
  820. u64 cmp_l = event->attr.config1;
  821. u64 cmp_h = event->attr.config2;
  822. u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
  823. u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
  824. hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
  825. /* Direction (RX/TX), device (port) & virtual channel */
  826. val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
  827. val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
  828. CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
  829. val |= CCN_CONFIG_DIR(event->attr.config) <<
  830. CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
  831. val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
  832. CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
  833. val |= CCN_CONFIG_PORT(event->attr.config) <<
  834. CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
  835. val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
  836. CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
  837. val |= CCN_CONFIG_VC(event->attr.config) <<
  838. CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
  839. writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
  840. /* Comparison values */
  841. writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
  842. writel((cmp_l >> 32) & 0x7fffffff,
  843. source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
  844. writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
  845. writel((cmp_h >> 32) & 0x0fffffff,
  846. source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
  847. /* Mask */
  848. writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
  849. writel((mask_l >> 32) & 0x7fffffff,
  850. source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
  851. writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
  852. writel((mask_h >> 32) & 0x0fffffff,
  853. source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
  854. }
  855. static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
  856. {
  857. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  858. struct hw_perf_event *hw = &event->hw;
  859. struct arm_ccn_component *source =
  860. ccn->dt.pmu_counters[hw->idx].source;
  861. u32 val, id;
  862. hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
  863. id = (CCN_CONFIG_VC(event->attr.config) << 4) |
  864. (CCN_CONFIG_BUS(event->attr.config) << 3) |
  865. (CCN_CONFIG_EVENT(event->attr.config) << 0);
  866. val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
  867. val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
  868. CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
  869. val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
  870. writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
  871. }
  872. static void arm_ccn_pmu_node_event_config(struct perf_event *event)
  873. {
  874. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  875. struct hw_perf_event *hw = &event->hw;
  876. struct arm_ccn_component *source =
  877. ccn->dt.pmu_counters[hw->idx].source;
  878. u32 type = CCN_CONFIG_TYPE(event->attr.config);
  879. u32 val, port;
  880. port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
  881. hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
  882. hw->config_base);
  883. /* These *_event_sel regs should be identical, but let's make sure... */
  884. BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
  885. BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
  886. BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
  887. CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
  888. BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
  889. CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
  890. BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
  891. CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
  892. BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
  893. CCN_RNI_PMU_EVENT_SEL__ID__MASK);
  894. if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
  895. !arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
  896. return;
  897. /* Set the event id for the pre-allocated counter */
  898. val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
  899. val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
  900. CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
  901. val |= CCN_CONFIG_EVENT(event->attr.config) <<
  902. CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
  903. writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
  904. }
  905. static void arm_ccn_pmu_event_config(struct perf_event *event)
  906. {
  907. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  908. struct hw_perf_event *hw = &event->hw;
  909. u32 xp, offset, val;
  910. /* Cycle counter requires no setup */
  911. if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
  912. return;
  913. if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
  914. xp = CCN_CONFIG_XP(event->attr.config);
  915. else
  916. xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
  917. spin_lock(&ccn->dt.config_lock);
  918. /* Set the DT bus "distance" register */
  919. offset = (hw->idx / 4) * 4;
  920. val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
  921. val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
  922. CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
  923. val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
  924. writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
  925. if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
  926. if (CCN_CONFIG_EVENT(event->attr.config) ==
  927. CCN_EVENT_WATCHPOINT)
  928. arm_ccn_pmu_xp_watchpoint_config(event);
  929. else
  930. arm_ccn_pmu_xp_event_config(event);
  931. } else {
  932. arm_ccn_pmu_node_event_config(event);
  933. }
  934. spin_unlock(&ccn->dt.config_lock);
  935. }
  936. static int arm_ccn_pmu_active_counters(struct arm_ccn *ccn)
  937. {
  938. return bitmap_weight(ccn->dt.pmu_counters_mask,
  939. CCN_NUM_PMU_EVENT_COUNTERS + 1);
  940. }
  941. static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
  942. {
  943. int err;
  944. struct hw_perf_event *hw = &event->hw;
  945. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  946. err = arm_ccn_pmu_event_alloc(event);
  947. if (err)
  948. return err;
  949. /*
  950. * Pin the timer, so that the overflows are handled by the chosen
  951. * event->cpu (this is the same one as presented in "cpumask"
  952. * attribute).
  953. */
  954. if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 1)
  955. hrtimer_start(&ccn->dt.hrtimer, arm_ccn_pmu_timer_period(),
  956. HRTIMER_MODE_REL_PINNED);
  957. arm_ccn_pmu_event_config(event);
  958. hw->state = PERF_HES_STOPPED;
  959. if (flags & PERF_EF_START)
  960. arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
  961. return 0;
  962. }
  963. static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
  964. {
  965. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  966. arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
  967. arm_ccn_pmu_event_release(event);
  968. if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 0)
  969. hrtimer_cancel(&ccn->dt.hrtimer);
  970. }
  971. static void arm_ccn_pmu_event_read(struct perf_event *event)
  972. {
  973. arm_ccn_pmu_event_update(event);
  974. }
  975. static void arm_ccn_pmu_enable(struct pmu *pmu)
  976. {
  977. struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
  978. u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
  979. val |= CCN_DT_PMCR__PMU_EN;
  980. writel(val, ccn->dt.base + CCN_DT_PMCR);
  981. }
  982. static void arm_ccn_pmu_disable(struct pmu *pmu)
  983. {
  984. struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
  985. u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
  986. val &= ~CCN_DT_PMCR__PMU_EN;
  987. writel(val, ccn->dt.base + CCN_DT_PMCR);
  988. }
  989. static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
  990. {
  991. u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
  992. int idx;
  993. if (!pmovsr)
  994. return IRQ_NONE;
  995. writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
  996. BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
  997. for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
  998. struct perf_event *event = dt->pmu_counters[idx].event;
  999. int overflowed = pmovsr & BIT(idx);
  1000. WARN_ON_ONCE(overflowed && !event &&
  1001. idx != CCN_IDX_PMU_CYCLE_COUNTER);
  1002. if (!event || !overflowed)
  1003. continue;
  1004. arm_ccn_pmu_event_update(event);
  1005. }
  1006. return IRQ_HANDLED;
  1007. }
  1008. static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
  1009. {
  1010. struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
  1011. hrtimer);
  1012. unsigned long flags;
  1013. local_irq_save(flags);
  1014. arm_ccn_pmu_overflow_handler(dt);
  1015. local_irq_restore(flags);
  1016. hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
  1017. return HRTIMER_RESTART;
  1018. }
  1019. static int arm_ccn_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
  1020. {
  1021. struct arm_ccn_dt *dt = hlist_entry_safe(node, struct arm_ccn_dt, node);
  1022. struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
  1023. unsigned int target;
  1024. if (!cpumask_test_and_clear_cpu(cpu, &dt->cpu))
  1025. return 0;
  1026. target = cpumask_any_but(cpu_online_mask, cpu);
  1027. if (target >= nr_cpu_ids)
  1028. return 0;
  1029. perf_pmu_migrate_context(&dt->pmu, cpu, target);
  1030. cpumask_set_cpu(target, &dt->cpu);
  1031. if (ccn->irq)
  1032. WARN_ON(irq_set_affinity_hint(ccn->irq, &dt->cpu) != 0);
  1033. return 0;
  1034. }
  1035. static DEFINE_IDA(arm_ccn_pmu_ida);
  1036. static int arm_ccn_pmu_init(struct arm_ccn *ccn)
  1037. {
  1038. int i;
  1039. char *name;
  1040. int err;
  1041. /* Initialize DT subsystem */
  1042. ccn->dt.base = ccn->base + CCN_REGION_SIZE;
  1043. spin_lock_init(&ccn->dt.config_lock);
  1044. writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
  1045. writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
  1046. writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
  1047. ccn->dt.base + CCN_DT_PMCR);
  1048. writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
  1049. for (i = 0; i < ccn->num_xps; i++) {
  1050. writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
  1051. writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
  1052. CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
  1053. (CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
  1054. CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
  1055. CCN_XP_DT_CONTROL__DT_ENABLE,
  1056. ccn->xp[i].base + CCN_XP_DT_CONTROL);
  1057. }
  1058. ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
  1059. ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
  1060. ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
  1061. ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
  1062. ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
  1063. ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
  1064. ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
  1065. ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
  1066. /* Get a convenient /sys/event_source/devices/ name */
  1067. ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
  1068. if (ccn->dt.id == 0) {
  1069. name = "ccn";
  1070. } else {
  1071. name = devm_kasprintf(ccn->dev, GFP_KERNEL, "ccn_%d",
  1072. ccn->dt.id);
  1073. if (!name) {
  1074. err = -ENOMEM;
  1075. goto error_choose_name;
  1076. }
  1077. }
  1078. /* Perf driver registration */
  1079. ccn->dt.pmu = (struct pmu) {
  1080. .module = THIS_MODULE,
  1081. .attr_groups = arm_ccn_pmu_attr_groups,
  1082. .task_ctx_nr = perf_invalid_context,
  1083. .event_init = arm_ccn_pmu_event_init,
  1084. .add = arm_ccn_pmu_event_add,
  1085. .del = arm_ccn_pmu_event_del,
  1086. .start = arm_ccn_pmu_event_start,
  1087. .stop = arm_ccn_pmu_event_stop,
  1088. .read = arm_ccn_pmu_event_read,
  1089. .pmu_enable = arm_ccn_pmu_enable,
  1090. .pmu_disable = arm_ccn_pmu_disable,
  1091. };
  1092. /* No overflow interrupt? Have to use a timer instead. */
  1093. if (!ccn->irq) {
  1094. dev_info(ccn->dev, "No access to interrupts, using timer.\n");
  1095. hrtimer_init(&ccn->dt.hrtimer, CLOCK_MONOTONIC,
  1096. HRTIMER_MODE_REL);
  1097. ccn->dt.hrtimer.function = arm_ccn_pmu_timer_handler;
  1098. }
  1099. /* Pick one CPU which we will use to collect data from CCN... */
  1100. cpumask_set_cpu(get_cpu(), &ccn->dt.cpu);
  1101. /* Also make sure that the overflow interrupt is handled by this CPU */
  1102. if (ccn->irq) {
  1103. err = irq_set_affinity_hint(ccn->irq, &ccn->dt.cpu);
  1104. if (err) {
  1105. dev_err(ccn->dev, "Failed to set interrupt affinity!\n");
  1106. goto error_set_affinity;
  1107. }
  1108. }
  1109. err = perf_pmu_register(&ccn->dt.pmu, name, -1);
  1110. if (err)
  1111. goto error_pmu_register;
  1112. cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
  1113. &ccn->dt.node);
  1114. put_cpu();
  1115. return 0;
  1116. error_pmu_register:
  1117. error_set_affinity:
  1118. put_cpu();
  1119. error_choose_name:
  1120. ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
  1121. for (i = 0; i < ccn->num_xps; i++)
  1122. writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
  1123. writel(0, ccn->dt.base + CCN_DT_PMCR);
  1124. return err;
  1125. }
  1126. static void arm_ccn_pmu_cleanup(struct arm_ccn *ccn)
  1127. {
  1128. int i;
  1129. cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_CCN_ONLINE,
  1130. &ccn->dt.node);
  1131. if (ccn->irq)
  1132. irq_set_affinity_hint(ccn->irq, NULL);
  1133. for (i = 0; i < ccn->num_xps; i++)
  1134. writel(0, ccn->xp[i].base + CCN_XP_DT_CONTROL);
  1135. writel(0, ccn->dt.base + CCN_DT_PMCR);
  1136. perf_pmu_unregister(&ccn->dt.pmu);
  1137. ida_simple_remove(&arm_ccn_pmu_ida, ccn->dt.id);
  1138. }
  1139. static int arm_ccn_for_each_valid_region(struct arm_ccn *ccn,
  1140. int (*callback)(struct arm_ccn *ccn, int region,
  1141. void __iomem *base, u32 type, u32 id))
  1142. {
  1143. int region;
  1144. for (region = 0; region < CCN_NUM_REGIONS; region++) {
  1145. u32 val, type, id;
  1146. void __iomem *base;
  1147. int err;
  1148. val = readl(ccn->base + CCN_MN_OLY_COMP_LIST_63_0 +
  1149. 4 * (region / 32));
  1150. if (!(val & (1 << (region % 32))))
  1151. continue;
  1152. base = ccn->base + region * CCN_REGION_SIZE;
  1153. val = readl(base + CCN_ALL_OLY_ID);
  1154. type = (val >> CCN_ALL_OLY_ID__OLY_ID__SHIFT) &
  1155. CCN_ALL_OLY_ID__OLY_ID__MASK;
  1156. id = (val >> CCN_ALL_OLY_ID__NODE_ID__SHIFT) &
  1157. CCN_ALL_OLY_ID__NODE_ID__MASK;
  1158. err = callback(ccn, region, base, type, id);
  1159. if (err)
  1160. return err;
  1161. }
  1162. return 0;
  1163. }
  1164. static int arm_ccn_get_nodes_num(struct arm_ccn *ccn, int region,
  1165. void __iomem *base, u32 type, u32 id)
  1166. {
  1167. if (type == CCN_TYPE_XP && id >= ccn->num_xps)
  1168. ccn->num_xps = id + 1;
  1169. else if (id >= ccn->num_nodes)
  1170. ccn->num_nodes = id + 1;
  1171. return 0;
  1172. }
  1173. static int arm_ccn_init_nodes(struct arm_ccn *ccn, int region,
  1174. void __iomem *base, u32 type, u32 id)
  1175. {
  1176. struct arm_ccn_component *component;
  1177. dev_dbg(ccn->dev, "Region %d: id=%u, type=0x%02x\n", region, id, type);
  1178. switch (type) {
  1179. case CCN_TYPE_MN:
  1180. ccn->mn_id = id;
  1181. return 0;
  1182. case CCN_TYPE_DT:
  1183. return 0;
  1184. case CCN_TYPE_XP:
  1185. component = &ccn->xp[id];
  1186. break;
  1187. case CCN_TYPE_SBSX:
  1188. ccn->sbsx_present = 1;
  1189. component = &ccn->node[id];
  1190. break;
  1191. case CCN_TYPE_SBAS:
  1192. ccn->sbas_present = 1;
  1193. /* Fall-through */
  1194. default:
  1195. component = &ccn->node[id];
  1196. break;
  1197. }
  1198. component->base = base;
  1199. component->type = type;
  1200. return 0;
  1201. }
  1202. static irqreturn_t arm_ccn_error_handler(struct arm_ccn *ccn,
  1203. const u32 *err_sig_val)
  1204. {
  1205. /* This should be really handled by firmware... */
  1206. dev_err(ccn->dev, "Error reported in %08x%08x%08x%08x%08x%08x.\n",
  1207. err_sig_val[5], err_sig_val[4], err_sig_val[3],
  1208. err_sig_val[2], err_sig_val[1], err_sig_val[0]);
  1209. dev_err(ccn->dev, "Disabling interrupt generation for all errors.\n");
  1210. writel(CCN_MN_ERRINT_STATUS__ALL_ERRORS__DISABLE,
  1211. ccn->base + CCN_MN_ERRINT_STATUS);
  1212. return IRQ_HANDLED;
  1213. }
  1214. static irqreturn_t arm_ccn_irq_handler(int irq, void *dev_id)
  1215. {
  1216. irqreturn_t res = IRQ_NONE;
  1217. struct arm_ccn *ccn = dev_id;
  1218. u32 err_sig_val[6];
  1219. u32 err_or;
  1220. int i;
  1221. /* PMU overflow is a special case */
  1222. err_or = err_sig_val[0] = readl(ccn->base + CCN_MN_ERR_SIG_VAL_63_0);
  1223. if (err_or & CCN_MN_ERR_SIG_VAL_63_0__DT) {
  1224. err_or &= ~CCN_MN_ERR_SIG_VAL_63_0__DT;
  1225. res = arm_ccn_pmu_overflow_handler(&ccn->dt);
  1226. }
  1227. /* Have to read all err_sig_vals to clear them */
  1228. for (i = 1; i < ARRAY_SIZE(err_sig_val); i++) {
  1229. err_sig_val[i] = readl(ccn->base +
  1230. CCN_MN_ERR_SIG_VAL_63_0 + i * 4);
  1231. err_or |= err_sig_val[i];
  1232. }
  1233. if (err_or)
  1234. res |= arm_ccn_error_handler(ccn, err_sig_val);
  1235. if (res != IRQ_NONE)
  1236. writel(CCN_MN_ERRINT_STATUS__INTREQ__DESSERT,
  1237. ccn->base + CCN_MN_ERRINT_STATUS);
  1238. return res;
  1239. }
  1240. static int arm_ccn_probe(struct platform_device *pdev)
  1241. {
  1242. struct arm_ccn *ccn;
  1243. struct resource *res;
  1244. unsigned int irq;
  1245. int err;
  1246. ccn = devm_kzalloc(&pdev->dev, sizeof(*ccn), GFP_KERNEL);
  1247. if (!ccn)
  1248. return -ENOMEM;
  1249. ccn->dev = &pdev->dev;
  1250. platform_set_drvdata(pdev, ccn);
  1251. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1252. ccn->base = devm_ioremap_resource(ccn->dev, res);
  1253. if (IS_ERR(ccn->base))
  1254. return PTR_ERR(ccn->base);
  1255. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1256. if (!res)
  1257. return -EINVAL;
  1258. irq = res->start;
  1259. /* Check if we can use the interrupt */
  1260. writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
  1261. ccn->base + CCN_MN_ERRINT_STATUS);
  1262. if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
  1263. CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
  1264. /* Can set 'disable' bits, so can acknowledge interrupts */
  1265. writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
  1266. ccn->base + CCN_MN_ERRINT_STATUS);
  1267. err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler,
  1268. IRQF_NOBALANCING | IRQF_NO_THREAD,
  1269. dev_name(ccn->dev), ccn);
  1270. if (err)
  1271. return err;
  1272. ccn->irq = irq;
  1273. }
  1274. /* Build topology */
  1275. err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
  1276. if (err)
  1277. return err;
  1278. ccn->node = devm_kcalloc(ccn->dev, ccn->num_nodes, sizeof(*ccn->node),
  1279. GFP_KERNEL);
  1280. ccn->xp = devm_kcalloc(ccn->dev, ccn->num_xps, sizeof(*ccn->node),
  1281. GFP_KERNEL);
  1282. if (!ccn->node || !ccn->xp)
  1283. return -ENOMEM;
  1284. err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
  1285. if (err)
  1286. return err;
  1287. return arm_ccn_pmu_init(ccn);
  1288. }
  1289. static int arm_ccn_remove(struct platform_device *pdev)
  1290. {
  1291. struct arm_ccn *ccn = platform_get_drvdata(pdev);
  1292. arm_ccn_pmu_cleanup(ccn);
  1293. return 0;
  1294. }
  1295. static const struct of_device_id arm_ccn_match[] = {
  1296. { .compatible = "arm,ccn-502", },
  1297. { .compatible = "arm,ccn-504", },
  1298. {},
  1299. };
  1300. MODULE_DEVICE_TABLE(of, arm_ccn_match);
  1301. static struct platform_driver arm_ccn_driver = {
  1302. .driver = {
  1303. .name = "arm-ccn",
  1304. .of_match_table = arm_ccn_match,
  1305. },
  1306. .probe = arm_ccn_probe,
  1307. .remove = arm_ccn_remove,
  1308. };
  1309. static int __init arm_ccn_init(void)
  1310. {
  1311. int i, ret;
  1312. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE,
  1313. "perf/arm/ccn:online", NULL,
  1314. arm_ccn_pmu_offline_cpu);
  1315. if (ret)
  1316. return ret;
  1317. for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
  1318. arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
  1319. ret = platform_driver_register(&arm_ccn_driver);
  1320. if (ret)
  1321. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
  1322. return ret;
  1323. }
  1324. static void __exit arm_ccn_exit(void)
  1325. {
  1326. platform_driver_unregister(&arm_ccn_driver);
  1327. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
  1328. }
  1329. module_init(arm_ccn_init);
  1330. module_exit(arm_ccn_exit);
  1331. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  1332. MODULE_LICENSE("GPL v2");