arm-ccn.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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. if (!res)
  1253. return -EINVAL;
  1254. if (!devm_request_mem_region(ccn->dev, res->start,
  1255. resource_size(res), pdev->name))
  1256. return -EBUSY;
  1257. ccn->base = devm_ioremap(ccn->dev, res->start,
  1258. resource_size(res));
  1259. if (!ccn->base)
  1260. return -EFAULT;
  1261. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1262. if (!res)
  1263. return -EINVAL;
  1264. irq = res->start;
  1265. /* Check if we can use the interrupt */
  1266. writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLE,
  1267. ccn->base + CCN_MN_ERRINT_STATUS);
  1268. if (readl(ccn->base + CCN_MN_ERRINT_STATUS) &
  1269. CCN_MN_ERRINT_STATUS__PMU_EVENTS__DISABLED) {
  1270. /* Can set 'disable' bits, so can acknowledge interrupts */
  1271. writel(CCN_MN_ERRINT_STATUS__PMU_EVENTS__ENABLE,
  1272. ccn->base + CCN_MN_ERRINT_STATUS);
  1273. err = devm_request_irq(ccn->dev, irq, arm_ccn_irq_handler,
  1274. IRQF_NOBALANCING | IRQF_NO_THREAD,
  1275. dev_name(ccn->dev), ccn);
  1276. if (err)
  1277. return err;
  1278. ccn->irq = irq;
  1279. }
  1280. /* Build topology */
  1281. err = arm_ccn_for_each_valid_region(ccn, arm_ccn_get_nodes_num);
  1282. if (err)
  1283. return err;
  1284. ccn->node = devm_kcalloc(ccn->dev, ccn->num_nodes, sizeof(*ccn->node),
  1285. GFP_KERNEL);
  1286. ccn->xp = devm_kcalloc(ccn->dev, ccn->num_xps, sizeof(*ccn->node),
  1287. GFP_KERNEL);
  1288. if (!ccn->node || !ccn->xp)
  1289. return -ENOMEM;
  1290. err = arm_ccn_for_each_valid_region(ccn, arm_ccn_init_nodes);
  1291. if (err)
  1292. return err;
  1293. return arm_ccn_pmu_init(ccn);
  1294. }
  1295. static int arm_ccn_remove(struct platform_device *pdev)
  1296. {
  1297. struct arm_ccn *ccn = platform_get_drvdata(pdev);
  1298. arm_ccn_pmu_cleanup(ccn);
  1299. return 0;
  1300. }
  1301. static const struct of_device_id arm_ccn_match[] = {
  1302. { .compatible = "arm,ccn-502", },
  1303. { .compatible = "arm,ccn-504", },
  1304. {},
  1305. };
  1306. MODULE_DEVICE_TABLE(of, arm_ccn_match);
  1307. static struct platform_driver arm_ccn_driver = {
  1308. .driver = {
  1309. .name = "arm-ccn",
  1310. .of_match_table = arm_ccn_match,
  1311. },
  1312. .probe = arm_ccn_probe,
  1313. .remove = arm_ccn_remove,
  1314. };
  1315. static int __init arm_ccn_init(void)
  1316. {
  1317. int i, ret;
  1318. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE,
  1319. "perf/arm/ccn:online", NULL,
  1320. arm_ccn_pmu_offline_cpu);
  1321. if (ret)
  1322. return ret;
  1323. for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
  1324. arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
  1325. ret = platform_driver_register(&arm_ccn_driver);
  1326. if (ret)
  1327. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
  1328. return ret;
  1329. }
  1330. static void __exit arm_ccn_exit(void)
  1331. {
  1332. platform_driver_unregister(&arm_ccn_driver);
  1333. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
  1334. }
  1335. module_init(arm_ccn_init);
  1336. module_exit(arm_ccn_exit);
  1337. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  1338. MODULE_LICENSE("GPL v2");