arm-ccn.c 45 KB

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