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/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 hlist_node node;
  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. int mn_id;
  163. };
  164. static int arm_ccn_node_to_xp(int node)
  165. {
  166. return node / CCN_NUM_XP_PORTS;
  167. }
  168. static int arm_ccn_node_to_xp_port(int node)
  169. {
  170. return node % CCN_NUM_XP_PORTS;
  171. }
  172. /*
  173. * Bit shifts and masks in these defines must be kept in sync with
  174. * arm_ccn_pmu_config_set() and CCN_FORMAT_ATTRs below!
  175. */
  176. #define CCN_CONFIG_NODE(_config) (((_config) >> 0) & 0xff)
  177. #define CCN_CONFIG_XP(_config) (((_config) >> 0) & 0xff)
  178. #define CCN_CONFIG_TYPE(_config) (((_config) >> 8) & 0xff)
  179. #define CCN_CONFIG_EVENT(_config) (((_config) >> 16) & 0xff)
  180. #define CCN_CONFIG_PORT(_config) (((_config) >> 24) & 0x3)
  181. #define CCN_CONFIG_BUS(_config) (((_config) >> 24) & 0x3)
  182. #define CCN_CONFIG_VC(_config) (((_config) >> 26) & 0x7)
  183. #define CCN_CONFIG_DIR(_config) (((_config) >> 29) & 0x1)
  184. #define CCN_CONFIG_MASK(_config) (((_config) >> 30) & 0xf)
  185. static void arm_ccn_pmu_config_set(u64 *config, u32 node_xp, u32 type, u32 port)
  186. {
  187. *config &= ~((0xff << 0) | (0xff << 8) | (0x3 << 24));
  188. *config |= (node_xp << 0) | (type << 8) | (port << 24);
  189. }
  190. static ssize_t arm_ccn_pmu_format_show(struct device *dev,
  191. struct device_attribute *attr, char *buf)
  192. {
  193. struct dev_ext_attribute *ea = container_of(attr,
  194. struct dev_ext_attribute, attr);
  195. return snprintf(buf, PAGE_SIZE, "%s\n", (char *)ea->var);
  196. }
  197. #define CCN_FORMAT_ATTR(_name, _config) \
  198. struct dev_ext_attribute arm_ccn_pmu_format_attr_##_name = \
  199. { __ATTR(_name, S_IRUGO, arm_ccn_pmu_format_show, \
  200. NULL), _config }
  201. static CCN_FORMAT_ATTR(node, "config:0-7");
  202. static CCN_FORMAT_ATTR(xp, "config:0-7");
  203. static CCN_FORMAT_ATTR(type, "config:8-15");
  204. static CCN_FORMAT_ATTR(event, "config:16-23");
  205. static CCN_FORMAT_ATTR(port, "config:24-25");
  206. static CCN_FORMAT_ATTR(bus, "config:24-25");
  207. static CCN_FORMAT_ATTR(vc, "config:26-28");
  208. static CCN_FORMAT_ATTR(dir, "config:29-29");
  209. static CCN_FORMAT_ATTR(mask, "config:30-33");
  210. static CCN_FORMAT_ATTR(cmp_l, "config1:0-62");
  211. static CCN_FORMAT_ATTR(cmp_h, "config2:0-59");
  212. static struct attribute *arm_ccn_pmu_format_attrs[] = {
  213. &arm_ccn_pmu_format_attr_node.attr.attr,
  214. &arm_ccn_pmu_format_attr_xp.attr.attr,
  215. &arm_ccn_pmu_format_attr_type.attr.attr,
  216. &arm_ccn_pmu_format_attr_event.attr.attr,
  217. &arm_ccn_pmu_format_attr_port.attr.attr,
  218. &arm_ccn_pmu_format_attr_bus.attr.attr,
  219. &arm_ccn_pmu_format_attr_vc.attr.attr,
  220. &arm_ccn_pmu_format_attr_dir.attr.attr,
  221. &arm_ccn_pmu_format_attr_mask.attr.attr,
  222. &arm_ccn_pmu_format_attr_cmp_l.attr.attr,
  223. &arm_ccn_pmu_format_attr_cmp_h.attr.attr,
  224. NULL
  225. };
  226. static struct attribute_group arm_ccn_pmu_format_attr_group = {
  227. .name = "format",
  228. .attrs = arm_ccn_pmu_format_attrs,
  229. };
  230. struct arm_ccn_pmu_event {
  231. struct device_attribute attr;
  232. u32 type;
  233. u32 event;
  234. int num_ports;
  235. int num_vcs;
  236. const char *def;
  237. int mask;
  238. };
  239. #define CCN_EVENT_ATTR(_name) \
  240. __ATTR(_name, S_IRUGO, arm_ccn_pmu_event_show, NULL)
  241. /*
  242. * Events defined in TRM for MN, HN-I and SBSX are actually watchpoints set on
  243. * their ports in XP they are connected to. For the sake of usability they are
  244. * explicitly defined here (and translated into a relevant watchpoint in
  245. * arm_ccn_pmu_event_init()) so the user can easily request them without deep
  246. * knowledge of the flit format.
  247. */
  248. #define CCN_EVENT_MN(_name, _def, _mask) { .attr = CCN_EVENT_ATTR(mn_##_name), \
  249. .type = CCN_TYPE_MN, .event = CCN_EVENT_WATCHPOINT, \
  250. .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, \
  251. .def = _def, .mask = _mask, }
  252. #define CCN_EVENT_HNI(_name, _def, _mask) { \
  253. .attr = CCN_EVENT_ATTR(hni_##_name), .type = CCN_TYPE_HNI, \
  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_SBSX(_name, _def, _mask) { \
  257. .attr = CCN_EVENT_ATTR(sbsx_##_name), .type = CCN_TYPE_SBSX, \
  258. .event = CCN_EVENT_WATCHPOINT, .num_ports = CCN_NUM_XP_PORTS, \
  259. .num_vcs = CCN_NUM_VCS, .def = _def, .mask = _mask, }
  260. #define CCN_EVENT_HNF(_name, _event) { .attr = CCN_EVENT_ATTR(hnf_##_name), \
  261. .type = CCN_TYPE_HNF, .event = _event, }
  262. #define CCN_EVENT_XP(_name, _event) { .attr = CCN_EVENT_ATTR(xp_##_name), \
  263. .type = CCN_TYPE_XP, .event = _event, \
  264. .num_ports = CCN_NUM_XP_PORTS, .num_vcs = CCN_NUM_VCS, }
  265. /*
  266. * RN-I & RN-D (RN-D = RN-I + DVM) nodes have different type ID depending
  267. * on configuration. One of them is picked to represent the whole group,
  268. * as they all share the same event types.
  269. */
  270. #define CCN_EVENT_RNI(_name, _event) { .attr = CCN_EVENT_ATTR(rni_##_name), \
  271. .type = CCN_TYPE_RNI_3P, .event = _event, }
  272. #define CCN_EVENT_SBAS(_name, _event) { .attr = CCN_EVENT_ATTR(sbas_##_name), \
  273. .type = CCN_TYPE_SBAS, .event = _event, }
  274. #define CCN_EVENT_CYCLES(_name) { .attr = CCN_EVENT_ATTR(_name), \
  275. .type = CCN_TYPE_CYCLES }
  276. static ssize_t arm_ccn_pmu_event_show(struct device *dev,
  277. struct device_attribute *attr, char *buf)
  278. {
  279. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  280. struct arm_ccn_pmu_event *event = container_of(attr,
  281. struct arm_ccn_pmu_event, attr);
  282. ssize_t res;
  283. res = snprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
  284. if (event->event)
  285. res += snprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
  286. event->event);
  287. if (event->def)
  288. res += snprintf(buf + res, PAGE_SIZE - res, ",%s",
  289. event->def);
  290. if (event->mask)
  291. res += snprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
  292. event->mask);
  293. /* Arguments required by an event */
  294. switch (event->type) {
  295. case CCN_TYPE_CYCLES:
  296. break;
  297. case CCN_TYPE_XP:
  298. res += snprintf(buf + res, PAGE_SIZE - res,
  299. ",xp=?,vc=?");
  300. if (event->event == CCN_EVENT_WATCHPOINT)
  301. res += snprintf(buf + res, PAGE_SIZE - res,
  302. ",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
  303. else
  304. res += snprintf(buf + res, PAGE_SIZE - res,
  305. ",bus=?");
  306. break;
  307. case CCN_TYPE_MN:
  308. res += snprintf(buf + res, PAGE_SIZE - res, ",node=%d", ccn->mn_id);
  309. break;
  310. default:
  311. res += snprintf(buf + res, PAGE_SIZE - res, ",node=?");
  312. break;
  313. }
  314. res += snprintf(buf + res, PAGE_SIZE - res, "\n");
  315. return res;
  316. }
  317. static umode_t arm_ccn_pmu_events_is_visible(struct kobject *kobj,
  318. struct attribute *attr, int index)
  319. {
  320. struct device *dev = kobj_to_dev(kobj);
  321. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  322. struct device_attribute *dev_attr = container_of(attr,
  323. struct device_attribute, attr);
  324. struct arm_ccn_pmu_event *event = container_of(dev_attr,
  325. struct arm_ccn_pmu_event, attr);
  326. if (event->type == CCN_TYPE_SBAS && !ccn->sbas_present)
  327. return 0;
  328. if (event->type == CCN_TYPE_SBSX && !ccn->sbsx_present)
  329. return 0;
  330. return attr->mode;
  331. }
  332. static struct arm_ccn_pmu_event arm_ccn_pmu_events[] = {
  333. CCN_EVENT_MN(eobarrier, "dir=1,vc=0,cmp_h=0x1c00", CCN_IDX_MASK_OPCODE),
  334. CCN_EVENT_MN(ecbarrier, "dir=1,vc=0,cmp_h=0x1e00", CCN_IDX_MASK_OPCODE),
  335. CCN_EVENT_MN(dvmop, "dir=1,vc=0,cmp_h=0x2800", CCN_IDX_MASK_OPCODE),
  336. CCN_EVENT_HNI(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
  337. CCN_EVENT_HNI(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
  338. CCN_EVENT_HNI(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
  339. CCN_EVENT_HNI(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
  340. CCN_EVENT_HNI(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
  341. CCN_IDX_MASK_ORDER),
  342. CCN_EVENT_SBSX(txdatflits, "dir=1,vc=3", CCN_IDX_MASK_ANY),
  343. CCN_EVENT_SBSX(rxdatflits, "dir=0,vc=3", CCN_IDX_MASK_ANY),
  344. CCN_EVENT_SBSX(txreqflits, "dir=1,vc=0", CCN_IDX_MASK_ANY),
  345. CCN_EVENT_SBSX(rxreqflits, "dir=0,vc=0", CCN_IDX_MASK_ANY),
  346. CCN_EVENT_SBSX(rxreqflits_order, "dir=0,vc=0,cmp_h=0x8000",
  347. CCN_IDX_MASK_ORDER),
  348. CCN_EVENT_HNF(cache_miss, 0x1),
  349. CCN_EVENT_HNF(l3_sf_cache_access, 0x02),
  350. CCN_EVENT_HNF(cache_fill, 0x3),
  351. CCN_EVENT_HNF(pocq_retry, 0x4),
  352. CCN_EVENT_HNF(pocq_reqs_recvd, 0x5),
  353. CCN_EVENT_HNF(sf_hit, 0x6),
  354. CCN_EVENT_HNF(sf_evictions, 0x7),
  355. CCN_EVENT_HNF(snoops_sent, 0x8),
  356. CCN_EVENT_HNF(snoops_broadcast, 0x9),
  357. CCN_EVENT_HNF(l3_eviction, 0xa),
  358. CCN_EVENT_HNF(l3_fill_invalid_way, 0xb),
  359. CCN_EVENT_HNF(mc_retries, 0xc),
  360. CCN_EVENT_HNF(mc_reqs, 0xd),
  361. CCN_EVENT_HNF(qos_hh_retry, 0xe),
  362. CCN_EVENT_RNI(rdata_beats_p0, 0x1),
  363. CCN_EVENT_RNI(rdata_beats_p1, 0x2),
  364. CCN_EVENT_RNI(rdata_beats_p2, 0x3),
  365. CCN_EVENT_RNI(rxdat_flits, 0x4),
  366. CCN_EVENT_RNI(txdat_flits, 0x5),
  367. CCN_EVENT_RNI(txreq_flits, 0x6),
  368. CCN_EVENT_RNI(txreq_flits_retried, 0x7),
  369. CCN_EVENT_RNI(rrt_full, 0x8),
  370. CCN_EVENT_RNI(wrt_full, 0x9),
  371. CCN_EVENT_RNI(txreq_flits_replayed, 0xa),
  372. CCN_EVENT_XP(upload_starvation, 0x1),
  373. CCN_EVENT_XP(download_starvation, 0x2),
  374. CCN_EVENT_XP(respin, 0x3),
  375. CCN_EVENT_XP(valid_flit, 0x4),
  376. CCN_EVENT_XP(watchpoint, CCN_EVENT_WATCHPOINT),
  377. CCN_EVENT_SBAS(rdata_beats_p0, 0x1),
  378. CCN_EVENT_SBAS(rxdat_flits, 0x4),
  379. CCN_EVENT_SBAS(txdat_flits, 0x5),
  380. CCN_EVENT_SBAS(txreq_flits, 0x6),
  381. CCN_EVENT_SBAS(txreq_flits_retried, 0x7),
  382. CCN_EVENT_SBAS(rrt_full, 0x8),
  383. CCN_EVENT_SBAS(wrt_full, 0x9),
  384. CCN_EVENT_SBAS(txreq_flits_replayed, 0xa),
  385. CCN_EVENT_CYCLES(cycles),
  386. };
  387. /* Populated in arm_ccn_init() */
  388. static struct attribute
  389. *arm_ccn_pmu_events_attrs[ARRAY_SIZE(arm_ccn_pmu_events) + 1];
  390. static struct attribute_group arm_ccn_pmu_events_attr_group = {
  391. .name = "events",
  392. .is_visible = arm_ccn_pmu_events_is_visible,
  393. .attrs = arm_ccn_pmu_events_attrs,
  394. };
  395. static u64 *arm_ccn_pmu_get_cmp_mask(struct arm_ccn *ccn, const char *name)
  396. {
  397. unsigned long i;
  398. if (WARN_ON(!name || !name[0] || !isxdigit(name[0]) || !name[1]))
  399. return NULL;
  400. i = isdigit(name[0]) ? name[0] - '0' : 0xa + tolower(name[0]) - 'a';
  401. switch (name[1]) {
  402. case 'l':
  403. return &ccn->dt.cmp_mask[i].l;
  404. case 'h':
  405. return &ccn->dt.cmp_mask[i].h;
  406. default:
  407. return NULL;
  408. }
  409. }
  410. static ssize_t arm_ccn_pmu_cmp_mask_show(struct device *dev,
  411. struct device_attribute *attr, char *buf)
  412. {
  413. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  414. u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
  415. return mask ? snprintf(buf, PAGE_SIZE, "0x%016llx\n", *mask) : -EINVAL;
  416. }
  417. static ssize_t arm_ccn_pmu_cmp_mask_store(struct device *dev,
  418. struct device_attribute *attr, const char *buf, size_t count)
  419. {
  420. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  421. u64 *mask = arm_ccn_pmu_get_cmp_mask(ccn, attr->attr.name);
  422. int err = -EINVAL;
  423. if (mask)
  424. err = kstrtoull(buf, 0, mask);
  425. return err ? err : count;
  426. }
  427. #define CCN_CMP_MASK_ATTR(_name) \
  428. struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
  429. __ATTR(_name, S_IRUGO | S_IWUSR, \
  430. arm_ccn_pmu_cmp_mask_show, arm_ccn_pmu_cmp_mask_store)
  431. #define CCN_CMP_MASK_ATTR_RO(_name) \
  432. struct device_attribute arm_ccn_pmu_cmp_mask_attr_##_name = \
  433. __ATTR(_name, S_IRUGO, arm_ccn_pmu_cmp_mask_show, NULL)
  434. static CCN_CMP_MASK_ATTR(0l);
  435. static CCN_CMP_MASK_ATTR(0h);
  436. static CCN_CMP_MASK_ATTR(1l);
  437. static CCN_CMP_MASK_ATTR(1h);
  438. static CCN_CMP_MASK_ATTR(2l);
  439. static CCN_CMP_MASK_ATTR(2h);
  440. static CCN_CMP_MASK_ATTR(3l);
  441. static CCN_CMP_MASK_ATTR(3h);
  442. static CCN_CMP_MASK_ATTR(4l);
  443. static CCN_CMP_MASK_ATTR(4h);
  444. static CCN_CMP_MASK_ATTR(5l);
  445. static CCN_CMP_MASK_ATTR(5h);
  446. static CCN_CMP_MASK_ATTR(6l);
  447. static CCN_CMP_MASK_ATTR(6h);
  448. static CCN_CMP_MASK_ATTR(7l);
  449. static CCN_CMP_MASK_ATTR(7h);
  450. static CCN_CMP_MASK_ATTR_RO(8l);
  451. static CCN_CMP_MASK_ATTR_RO(8h);
  452. static CCN_CMP_MASK_ATTR_RO(9l);
  453. static CCN_CMP_MASK_ATTR_RO(9h);
  454. static CCN_CMP_MASK_ATTR_RO(al);
  455. static CCN_CMP_MASK_ATTR_RO(ah);
  456. static CCN_CMP_MASK_ATTR_RO(bl);
  457. static CCN_CMP_MASK_ATTR_RO(bh);
  458. static struct attribute *arm_ccn_pmu_cmp_mask_attrs[] = {
  459. &arm_ccn_pmu_cmp_mask_attr_0l.attr, &arm_ccn_pmu_cmp_mask_attr_0h.attr,
  460. &arm_ccn_pmu_cmp_mask_attr_1l.attr, &arm_ccn_pmu_cmp_mask_attr_1h.attr,
  461. &arm_ccn_pmu_cmp_mask_attr_2l.attr, &arm_ccn_pmu_cmp_mask_attr_2h.attr,
  462. &arm_ccn_pmu_cmp_mask_attr_3l.attr, &arm_ccn_pmu_cmp_mask_attr_3h.attr,
  463. &arm_ccn_pmu_cmp_mask_attr_4l.attr, &arm_ccn_pmu_cmp_mask_attr_4h.attr,
  464. &arm_ccn_pmu_cmp_mask_attr_5l.attr, &arm_ccn_pmu_cmp_mask_attr_5h.attr,
  465. &arm_ccn_pmu_cmp_mask_attr_6l.attr, &arm_ccn_pmu_cmp_mask_attr_6h.attr,
  466. &arm_ccn_pmu_cmp_mask_attr_7l.attr, &arm_ccn_pmu_cmp_mask_attr_7h.attr,
  467. &arm_ccn_pmu_cmp_mask_attr_8l.attr, &arm_ccn_pmu_cmp_mask_attr_8h.attr,
  468. &arm_ccn_pmu_cmp_mask_attr_9l.attr, &arm_ccn_pmu_cmp_mask_attr_9h.attr,
  469. &arm_ccn_pmu_cmp_mask_attr_al.attr, &arm_ccn_pmu_cmp_mask_attr_ah.attr,
  470. &arm_ccn_pmu_cmp_mask_attr_bl.attr, &arm_ccn_pmu_cmp_mask_attr_bh.attr,
  471. NULL
  472. };
  473. static struct attribute_group arm_ccn_pmu_cmp_mask_attr_group = {
  474. .name = "cmp_mask",
  475. .attrs = arm_ccn_pmu_cmp_mask_attrs,
  476. };
  477. static ssize_t arm_ccn_pmu_cpumask_show(struct device *dev,
  478. struct device_attribute *attr, char *buf)
  479. {
  480. struct arm_ccn *ccn = pmu_to_arm_ccn(dev_get_drvdata(dev));
  481. return cpumap_print_to_pagebuf(true, buf, &ccn->dt.cpu);
  482. }
  483. static struct device_attribute arm_ccn_pmu_cpumask_attr =
  484. __ATTR(cpumask, S_IRUGO, arm_ccn_pmu_cpumask_show, NULL);
  485. static struct attribute *arm_ccn_pmu_cpumask_attrs[] = {
  486. &arm_ccn_pmu_cpumask_attr.attr,
  487. NULL,
  488. };
  489. static struct attribute_group arm_ccn_pmu_cpumask_attr_group = {
  490. .attrs = arm_ccn_pmu_cpumask_attrs,
  491. };
  492. /*
  493. * Default poll period is 10ms, which is way over the top anyway,
  494. * as in the worst case scenario (an event every cycle), with 1GHz
  495. * clocked bus, the smallest, 32 bit counter will overflow in
  496. * more than 4s.
  497. */
  498. static unsigned int arm_ccn_pmu_poll_period_us = 10000;
  499. module_param_named(pmu_poll_period_us, arm_ccn_pmu_poll_period_us, uint,
  500. S_IRUGO | S_IWUSR);
  501. static ktime_t arm_ccn_pmu_timer_period(void)
  502. {
  503. return ns_to_ktime((u64)arm_ccn_pmu_poll_period_us * 1000);
  504. }
  505. static const struct attribute_group *arm_ccn_pmu_attr_groups[] = {
  506. &arm_ccn_pmu_events_attr_group,
  507. &arm_ccn_pmu_format_attr_group,
  508. &arm_ccn_pmu_cmp_mask_attr_group,
  509. &arm_ccn_pmu_cpumask_attr_group,
  510. NULL
  511. };
  512. static int arm_ccn_pmu_alloc_bit(unsigned long *bitmap, unsigned long size)
  513. {
  514. int bit;
  515. do {
  516. bit = find_first_zero_bit(bitmap, size);
  517. if (bit >= size)
  518. return -EAGAIN;
  519. } while (test_and_set_bit(bit, bitmap));
  520. return bit;
  521. }
  522. /* All RN-I and RN-D nodes have identical PMUs */
  523. static int arm_ccn_pmu_type_eq(u32 a, u32 b)
  524. {
  525. if (a == b)
  526. return 1;
  527. switch (a) {
  528. case CCN_TYPE_RNI_1P:
  529. case CCN_TYPE_RNI_2P:
  530. case CCN_TYPE_RNI_3P:
  531. case CCN_TYPE_RND_1P:
  532. case CCN_TYPE_RND_2P:
  533. case CCN_TYPE_RND_3P:
  534. switch (b) {
  535. case CCN_TYPE_RNI_1P:
  536. case CCN_TYPE_RNI_2P:
  537. case CCN_TYPE_RNI_3P:
  538. case CCN_TYPE_RND_1P:
  539. case CCN_TYPE_RND_2P:
  540. case CCN_TYPE_RND_3P:
  541. return 1;
  542. }
  543. break;
  544. }
  545. return 0;
  546. }
  547. static int arm_ccn_pmu_event_alloc(struct perf_event *event)
  548. {
  549. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  550. struct hw_perf_event *hw = &event->hw;
  551. u32 node_xp, type, event_id;
  552. struct arm_ccn_component *source;
  553. int bit;
  554. node_xp = CCN_CONFIG_NODE(event->attr.config);
  555. type = CCN_CONFIG_TYPE(event->attr.config);
  556. event_id = CCN_CONFIG_EVENT(event->attr.config);
  557. /* Allocate the cycle counter */
  558. if (type == CCN_TYPE_CYCLES) {
  559. if (test_and_set_bit(CCN_IDX_PMU_CYCLE_COUNTER,
  560. ccn->dt.pmu_counters_mask))
  561. return -EAGAIN;
  562. hw->idx = CCN_IDX_PMU_CYCLE_COUNTER;
  563. ccn->dt.pmu_counters[CCN_IDX_PMU_CYCLE_COUNTER].event = event;
  564. return 0;
  565. }
  566. /* Allocate an event counter */
  567. hw->idx = arm_ccn_pmu_alloc_bit(ccn->dt.pmu_counters_mask,
  568. CCN_NUM_PMU_EVENT_COUNTERS);
  569. if (hw->idx < 0) {
  570. dev_dbg(ccn->dev, "No more counters available!\n");
  571. return -EAGAIN;
  572. }
  573. if (type == CCN_TYPE_XP)
  574. source = &ccn->xp[node_xp];
  575. else
  576. source = &ccn->node[node_xp];
  577. ccn->dt.pmu_counters[hw->idx].source = source;
  578. /* Allocate an event source or a watchpoint */
  579. if (type == CCN_TYPE_XP && event_id == CCN_EVENT_WATCHPOINT)
  580. bit = arm_ccn_pmu_alloc_bit(source->xp.dt_cmp_mask,
  581. CCN_NUM_XP_WATCHPOINTS);
  582. else
  583. bit = arm_ccn_pmu_alloc_bit(source->pmu_events_mask,
  584. CCN_NUM_PMU_EVENTS);
  585. if (bit < 0) {
  586. dev_dbg(ccn->dev, "No more event sources/watchpoints on node/XP %d!\n",
  587. node_xp);
  588. clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
  589. return -EAGAIN;
  590. }
  591. hw->config_base = bit;
  592. ccn->dt.pmu_counters[hw->idx].event = event;
  593. return 0;
  594. }
  595. static void arm_ccn_pmu_event_release(struct perf_event *event)
  596. {
  597. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  598. struct hw_perf_event *hw = &event->hw;
  599. if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER) {
  600. clear_bit(CCN_IDX_PMU_CYCLE_COUNTER, ccn->dt.pmu_counters_mask);
  601. } else {
  602. struct arm_ccn_component *source =
  603. ccn->dt.pmu_counters[hw->idx].source;
  604. if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP &&
  605. CCN_CONFIG_EVENT(event->attr.config) ==
  606. CCN_EVENT_WATCHPOINT)
  607. clear_bit(hw->config_base, source->xp.dt_cmp_mask);
  608. else
  609. clear_bit(hw->config_base, source->pmu_events_mask);
  610. clear_bit(hw->idx, ccn->dt.pmu_counters_mask);
  611. }
  612. ccn->dt.pmu_counters[hw->idx].source = NULL;
  613. ccn->dt.pmu_counters[hw->idx].event = NULL;
  614. }
  615. static int arm_ccn_pmu_event_init(struct perf_event *event)
  616. {
  617. struct arm_ccn *ccn;
  618. struct hw_perf_event *hw = &event->hw;
  619. u32 node_xp, type, event_id;
  620. int valid;
  621. int i;
  622. struct perf_event *sibling;
  623. if (event->attr.type != event->pmu->type)
  624. return -ENOENT;
  625. ccn = pmu_to_arm_ccn(event->pmu);
  626. if (hw->sample_period) {
  627. dev_dbg(ccn->dev, "Sampling not supported!\n");
  628. return -EOPNOTSUPP;
  629. }
  630. if (has_branch_stack(event) || event->attr.exclude_user ||
  631. event->attr.exclude_kernel || event->attr.exclude_hv ||
  632. event->attr.exclude_idle || event->attr.exclude_host ||
  633. event->attr.exclude_guest) {
  634. dev_dbg(ccn->dev, "Can't exclude execution levels!\n");
  635. return -EINVAL;
  636. }
  637. if (event->cpu < 0) {
  638. dev_dbg(ccn->dev, "Can't provide per-task data!\n");
  639. return -EOPNOTSUPP;
  640. }
  641. /*
  642. * Many perf core operations (eg. events rotation) operate on a
  643. * single CPU context. This is obvious for CPU PMUs, where one
  644. * expects the same sets of events being observed on all CPUs,
  645. * but can lead to issues for off-core PMUs, like CCN, where each
  646. * event could be theoretically assigned to a different CPU. To
  647. * mitigate this, we enforce CPU assignment to one, selected
  648. * processor (the one described in the "cpumask" attribute).
  649. */
  650. event->cpu = cpumask_first(&ccn->dt.cpu);
  651. node_xp = CCN_CONFIG_NODE(event->attr.config);
  652. type = CCN_CONFIG_TYPE(event->attr.config);
  653. event_id = CCN_CONFIG_EVENT(event->attr.config);
  654. /* Validate node/xp vs topology */
  655. switch (type) {
  656. case CCN_TYPE_MN:
  657. if (node_xp != ccn->mn_id) {
  658. dev_dbg(ccn->dev, "Invalid MN ID %d!\n", node_xp);
  659. return -EINVAL;
  660. }
  661. break;
  662. case CCN_TYPE_XP:
  663. if (node_xp >= ccn->num_xps) {
  664. dev_dbg(ccn->dev, "Invalid XP ID %d!\n", node_xp);
  665. return -EINVAL;
  666. }
  667. break;
  668. case CCN_TYPE_CYCLES:
  669. break;
  670. default:
  671. if (node_xp >= ccn->num_nodes) {
  672. dev_dbg(ccn->dev, "Invalid node ID %d!\n", node_xp);
  673. return -EINVAL;
  674. }
  675. if (!arm_ccn_pmu_type_eq(type, ccn->node[node_xp].type)) {
  676. dev_dbg(ccn->dev, "Invalid type 0x%x for node %d!\n",
  677. type, node_xp);
  678. return -EINVAL;
  679. }
  680. break;
  681. }
  682. /* Validate event ID vs available for the type */
  683. for (i = 0, valid = 0; i < ARRAY_SIZE(arm_ccn_pmu_events) && !valid;
  684. i++) {
  685. struct arm_ccn_pmu_event *e = &arm_ccn_pmu_events[i];
  686. u32 port = CCN_CONFIG_PORT(event->attr.config);
  687. u32 vc = CCN_CONFIG_VC(event->attr.config);
  688. if (!arm_ccn_pmu_type_eq(type, e->type))
  689. continue;
  690. if (event_id != e->event)
  691. continue;
  692. if (e->num_ports && port >= e->num_ports) {
  693. dev_dbg(ccn->dev, "Invalid port %d for node/XP %d!\n",
  694. port, node_xp);
  695. return -EINVAL;
  696. }
  697. if (e->num_vcs && vc >= e->num_vcs) {
  698. dev_dbg(ccn->dev, "Invalid vc %d for node/XP %d!\n",
  699. vc, node_xp);
  700. return -EINVAL;
  701. }
  702. valid = 1;
  703. }
  704. if (!valid) {
  705. dev_dbg(ccn->dev, "Invalid event 0x%x for node/XP %d!\n",
  706. event_id, node_xp);
  707. return -EINVAL;
  708. }
  709. /* Watchpoint-based event for a node is actually set on XP */
  710. if (event_id == CCN_EVENT_WATCHPOINT && type != CCN_TYPE_XP) {
  711. u32 port;
  712. type = CCN_TYPE_XP;
  713. port = arm_ccn_node_to_xp_port(node_xp);
  714. node_xp = arm_ccn_node_to_xp(node_xp);
  715. arm_ccn_pmu_config_set(&event->attr.config,
  716. node_xp, type, port);
  717. }
  718. /*
  719. * We must NOT create groups containing mixed PMUs, although software
  720. * events are acceptable (for example to create a CCN group
  721. * periodically read when a hrtimer aka cpu-clock leader triggers).
  722. */
  723. if (event->group_leader->pmu != event->pmu &&
  724. !is_software_event(event->group_leader))
  725. return -EINVAL;
  726. list_for_each_entry(sibling, &event->group_leader->sibling_list,
  727. group_entry)
  728. if (sibling->pmu != event->pmu &&
  729. !is_software_event(sibling))
  730. return -EINVAL;
  731. return 0;
  732. }
  733. static u64 arm_ccn_pmu_read_counter(struct arm_ccn *ccn, int idx)
  734. {
  735. u64 res;
  736. if (idx == CCN_IDX_PMU_CYCLE_COUNTER) {
  737. #ifdef readq
  738. res = readq(ccn->dt.base + CCN_DT_PMCCNTR);
  739. #else
  740. /* 40 bit counter, can do snapshot and read in two parts */
  741. writel(0x1, ccn->dt.base + CCN_DT_PMSR_REQ);
  742. while (!(readl(ccn->dt.base + CCN_DT_PMSR) & 0x1))
  743. ;
  744. writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
  745. res = readl(ccn->dt.base + CCN_DT_PMCCNTRSR + 4) & 0xff;
  746. res <<= 32;
  747. res |= readl(ccn->dt.base + CCN_DT_PMCCNTRSR);
  748. #endif
  749. } else {
  750. res = readl(ccn->dt.base + CCN_DT_PMEVCNT(idx));
  751. }
  752. return res;
  753. }
  754. static void arm_ccn_pmu_event_update(struct perf_event *event)
  755. {
  756. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  757. struct hw_perf_event *hw = &event->hw;
  758. u64 prev_count, new_count, mask;
  759. do {
  760. prev_count = local64_read(&hw->prev_count);
  761. new_count = arm_ccn_pmu_read_counter(ccn, hw->idx);
  762. } while (local64_xchg(&hw->prev_count, new_count) != prev_count);
  763. mask = (1LLU << (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER ? 40 : 32)) - 1;
  764. local64_add((new_count - prev_count) & mask, &event->count);
  765. }
  766. static void arm_ccn_pmu_xp_dt_config(struct perf_event *event, int enable)
  767. {
  768. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  769. struct hw_perf_event *hw = &event->hw;
  770. struct arm_ccn_component *xp;
  771. u32 val, dt_cfg;
  772. /* Nothing to do for cycle counter */
  773. if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
  774. return;
  775. if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
  776. xp = &ccn->xp[CCN_CONFIG_XP(event->attr.config)];
  777. else
  778. xp = &ccn->xp[arm_ccn_node_to_xp(
  779. CCN_CONFIG_NODE(event->attr.config))];
  780. if (enable)
  781. dt_cfg = hw->event_base;
  782. else
  783. dt_cfg = CCN_XP_DT_CONFIG__DT_CFG__PASS_THROUGH;
  784. spin_lock(&ccn->dt.config_lock);
  785. val = readl(xp->base + CCN_XP_DT_CONFIG);
  786. val &= ~(CCN_XP_DT_CONFIG__DT_CFG__MASK <<
  787. CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx));
  788. val |= dt_cfg << CCN_XP_DT_CONFIG__DT_CFG__SHIFT(hw->idx);
  789. writel(val, xp->base + CCN_XP_DT_CONFIG);
  790. spin_unlock(&ccn->dt.config_lock);
  791. }
  792. static void arm_ccn_pmu_event_start(struct perf_event *event, int flags)
  793. {
  794. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  795. struct hw_perf_event *hw = &event->hw;
  796. local64_set(&event->hw.prev_count,
  797. arm_ccn_pmu_read_counter(ccn, hw->idx));
  798. hw->state = 0;
  799. /* Set the DT bus input, engaging the counter */
  800. arm_ccn_pmu_xp_dt_config(event, 1);
  801. }
  802. static void arm_ccn_pmu_event_stop(struct perf_event *event, int flags)
  803. {
  804. struct hw_perf_event *hw = &event->hw;
  805. /* Disable counting, setting the DT bus to pass-through mode */
  806. arm_ccn_pmu_xp_dt_config(event, 0);
  807. if (flags & PERF_EF_UPDATE)
  808. arm_ccn_pmu_event_update(event);
  809. hw->state |= PERF_HES_STOPPED;
  810. }
  811. static void arm_ccn_pmu_xp_watchpoint_config(struct perf_event *event)
  812. {
  813. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  814. struct hw_perf_event *hw = &event->hw;
  815. struct arm_ccn_component *source =
  816. ccn->dt.pmu_counters[hw->idx].source;
  817. unsigned long wp = hw->config_base;
  818. u32 val;
  819. u64 cmp_l = event->attr.config1;
  820. u64 cmp_h = event->attr.config2;
  821. u64 mask_l = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].l;
  822. u64 mask_h = ccn->dt.cmp_mask[CCN_CONFIG_MASK(event->attr.config)].h;
  823. hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__WATCHPOINT(wp);
  824. /* Direction (RX/TX), device (port) & virtual channel */
  825. val = readl(source->base + CCN_XP_DT_INTERFACE_SEL);
  826. val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__MASK <<
  827. CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp));
  828. val |= CCN_CONFIG_DIR(event->attr.config) <<
  829. CCN_XP_DT_INTERFACE_SEL__DT_IO_SEL__SHIFT(wp);
  830. val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__MASK <<
  831. CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp));
  832. val |= CCN_CONFIG_PORT(event->attr.config) <<
  833. CCN_XP_DT_INTERFACE_SEL__DT_DEV_SEL__SHIFT(wp);
  834. val &= ~(CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__MASK <<
  835. CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp));
  836. val |= CCN_CONFIG_VC(event->attr.config) <<
  837. CCN_XP_DT_INTERFACE_SEL__DT_VC_SEL__SHIFT(wp);
  838. writel(val, source->base + CCN_XP_DT_INTERFACE_SEL);
  839. /* Comparison values */
  840. writel(cmp_l & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_L(wp));
  841. writel((cmp_l >> 32) & 0x7fffffff,
  842. source->base + CCN_XP_DT_CMP_VAL_L(wp) + 4);
  843. writel(cmp_h & 0xffffffff, source->base + CCN_XP_DT_CMP_VAL_H(wp));
  844. writel((cmp_h >> 32) & 0x0fffffff,
  845. source->base + CCN_XP_DT_CMP_VAL_H(wp) + 4);
  846. /* Mask */
  847. writel(mask_l & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_L(wp));
  848. writel((mask_l >> 32) & 0x7fffffff,
  849. source->base + CCN_XP_DT_CMP_MASK_L(wp) + 4);
  850. writel(mask_h & 0xffffffff, source->base + CCN_XP_DT_CMP_MASK_H(wp));
  851. writel((mask_h >> 32) & 0x0fffffff,
  852. source->base + CCN_XP_DT_CMP_MASK_H(wp) + 4);
  853. }
  854. static void arm_ccn_pmu_xp_event_config(struct perf_event *event)
  855. {
  856. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  857. struct hw_perf_event *hw = &event->hw;
  858. struct arm_ccn_component *source =
  859. ccn->dt.pmu_counters[hw->idx].source;
  860. u32 val, id;
  861. hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__XP_PMU_EVENT(hw->config_base);
  862. id = (CCN_CONFIG_VC(event->attr.config) << 4) |
  863. (CCN_CONFIG_BUS(event->attr.config) << 3) |
  864. (CCN_CONFIG_EVENT(event->attr.config) << 0);
  865. val = readl(source->base + CCN_XP_PMU_EVENT_SEL);
  866. val &= ~(CCN_XP_PMU_EVENT_SEL__ID__MASK <<
  867. CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
  868. val |= id << CCN_XP_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
  869. writel(val, source->base + CCN_XP_PMU_EVENT_SEL);
  870. }
  871. static void arm_ccn_pmu_node_event_config(struct perf_event *event)
  872. {
  873. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  874. struct hw_perf_event *hw = &event->hw;
  875. struct arm_ccn_component *source =
  876. ccn->dt.pmu_counters[hw->idx].source;
  877. u32 type = CCN_CONFIG_TYPE(event->attr.config);
  878. u32 val, port;
  879. port = arm_ccn_node_to_xp_port(CCN_CONFIG_NODE(event->attr.config));
  880. hw->event_base = CCN_XP_DT_CONFIG__DT_CFG__DEVICE_PMU_EVENT(port,
  881. hw->config_base);
  882. /* These *_event_sel regs should be identical, but let's make sure... */
  883. BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL != CCN_SBAS_PMU_EVENT_SEL);
  884. BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL != CCN_RNI_PMU_EVENT_SEL);
  885. BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(1) !=
  886. CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1));
  887. BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__SHIFT(1) !=
  888. CCN_RNI_PMU_EVENT_SEL__ID__SHIFT(1));
  889. BUILD_BUG_ON(CCN_HNF_PMU_EVENT_SEL__ID__MASK !=
  890. CCN_SBAS_PMU_EVENT_SEL__ID__MASK);
  891. BUILD_BUG_ON(CCN_SBAS_PMU_EVENT_SEL__ID__MASK !=
  892. CCN_RNI_PMU_EVENT_SEL__ID__MASK);
  893. if (WARN_ON(type != CCN_TYPE_HNF && type != CCN_TYPE_SBAS &&
  894. !arm_ccn_pmu_type_eq(type, CCN_TYPE_RNI_3P)))
  895. return;
  896. /* Set the event id for the pre-allocated counter */
  897. val = readl(source->base + CCN_HNF_PMU_EVENT_SEL);
  898. val &= ~(CCN_HNF_PMU_EVENT_SEL__ID__MASK <<
  899. CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base));
  900. val |= CCN_CONFIG_EVENT(event->attr.config) <<
  901. CCN_HNF_PMU_EVENT_SEL__ID__SHIFT(hw->config_base);
  902. writel(val, source->base + CCN_HNF_PMU_EVENT_SEL);
  903. }
  904. static void arm_ccn_pmu_event_config(struct perf_event *event)
  905. {
  906. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  907. struct hw_perf_event *hw = &event->hw;
  908. u32 xp, offset, val;
  909. /* Cycle counter requires no setup */
  910. if (hw->idx == CCN_IDX_PMU_CYCLE_COUNTER)
  911. return;
  912. if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP)
  913. xp = CCN_CONFIG_XP(event->attr.config);
  914. else
  915. xp = arm_ccn_node_to_xp(CCN_CONFIG_NODE(event->attr.config));
  916. spin_lock(&ccn->dt.config_lock);
  917. /* Set the DT bus "distance" register */
  918. offset = (hw->idx / 4) * 4;
  919. val = readl(ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
  920. val &= ~(CCN_DT_ACTIVE_DSM__DSM_ID__MASK <<
  921. CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4));
  922. val |= xp << CCN_DT_ACTIVE_DSM__DSM_ID__SHIFT(hw->idx % 4);
  923. writel(val, ccn->dt.base + CCN_DT_ACTIVE_DSM + offset);
  924. if (CCN_CONFIG_TYPE(event->attr.config) == CCN_TYPE_XP) {
  925. if (CCN_CONFIG_EVENT(event->attr.config) ==
  926. CCN_EVENT_WATCHPOINT)
  927. arm_ccn_pmu_xp_watchpoint_config(event);
  928. else
  929. arm_ccn_pmu_xp_event_config(event);
  930. } else {
  931. arm_ccn_pmu_node_event_config(event);
  932. }
  933. spin_unlock(&ccn->dt.config_lock);
  934. }
  935. static int arm_ccn_pmu_active_counters(struct arm_ccn *ccn)
  936. {
  937. return bitmap_weight(ccn->dt.pmu_counters_mask,
  938. CCN_NUM_PMU_EVENT_COUNTERS + 1);
  939. }
  940. static int arm_ccn_pmu_event_add(struct perf_event *event, int flags)
  941. {
  942. int err;
  943. struct hw_perf_event *hw = &event->hw;
  944. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  945. err = arm_ccn_pmu_event_alloc(event);
  946. if (err)
  947. return err;
  948. /*
  949. * Pin the timer, so that the overflows are handled by the chosen
  950. * event->cpu (this is the same one as presented in "cpumask"
  951. * attribute).
  952. */
  953. if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 1)
  954. hrtimer_start(&ccn->dt.hrtimer, arm_ccn_pmu_timer_period(),
  955. HRTIMER_MODE_REL_PINNED);
  956. arm_ccn_pmu_event_config(event);
  957. hw->state = PERF_HES_STOPPED;
  958. if (flags & PERF_EF_START)
  959. arm_ccn_pmu_event_start(event, PERF_EF_UPDATE);
  960. return 0;
  961. }
  962. static void arm_ccn_pmu_event_del(struct perf_event *event, int flags)
  963. {
  964. struct arm_ccn *ccn = pmu_to_arm_ccn(event->pmu);
  965. arm_ccn_pmu_event_stop(event, PERF_EF_UPDATE);
  966. arm_ccn_pmu_event_release(event);
  967. if (!ccn->irq && arm_ccn_pmu_active_counters(ccn) == 0)
  968. hrtimer_cancel(&ccn->dt.hrtimer);
  969. }
  970. static void arm_ccn_pmu_event_read(struct perf_event *event)
  971. {
  972. arm_ccn_pmu_event_update(event);
  973. }
  974. static void arm_ccn_pmu_enable(struct pmu *pmu)
  975. {
  976. struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
  977. u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
  978. val |= CCN_DT_PMCR__PMU_EN;
  979. writel(val, ccn->dt.base + CCN_DT_PMCR);
  980. }
  981. static void arm_ccn_pmu_disable(struct pmu *pmu)
  982. {
  983. struct arm_ccn *ccn = pmu_to_arm_ccn(pmu);
  984. u32 val = readl(ccn->dt.base + CCN_DT_PMCR);
  985. val &= ~CCN_DT_PMCR__PMU_EN;
  986. writel(val, ccn->dt.base + CCN_DT_PMCR);
  987. }
  988. static irqreturn_t arm_ccn_pmu_overflow_handler(struct arm_ccn_dt *dt)
  989. {
  990. u32 pmovsr = readl(dt->base + CCN_DT_PMOVSR);
  991. int idx;
  992. if (!pmovsr)
  993. return IRQ_NONE;
  994. writel(pmovsr, dt->base + CCN_DT_PMOVSR_CLR);
  995. BUILD_BUG_ON(CCN_IDX_PMU_CYCLE_COUNTER != CCN_NUM_PMU_EVENT_COUNTERS);
  996. for (idx = 0; idx < CCN_NUM_PMU_EVENT_COUNTERS + 1; idx++) {
  997. struct perf_event *event = dt->pmu_counters[idx].event;
  998. int overflowed = pmovsr & BIT(idx);
  999. WARN_ON_ONCE(overflowed && !event &&
  1000. idx != CCN_IDX_PMU_CYCLE_COUNTER);
  1001. if (!event || !overflowed)
  1002. continue;
  1003. arm_ccn_pmu_event_update(event);
  1004. }
  1005. return IRQ_HANDLED;
  1006. }
  1007. static enum hrtimer_restart arm_ccn_pmu_timer_handler(struct hrtimer *hrtimer)
  1008. {
  1009. struct arm_ccn_dt *dt = container_of(hrtimer, struct arm_ccn_dt,
  1010. hrtimer);
  1011. unsigned long flags;
  1012. local_irq_save(flags);
  1013. arm_ccn_pmu_overflow_handler(dt);
  1014. local_irq_restore(flags);
  1015. hrtimer_forward_now(hrtimer, arm_ccn_pmu_timer_period());
  1016. return HRTIMER_RESTART;
  1017. }
  1018. static int arm_ccn_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
  1019. {
  1020. struct arm_ccn_dt *dt = hlist_entry_safe(node, struct arm_ccn_dt, node);
  1021. struct arm_ccn *ccn = container_of(dt, struct arm_ccn, dt);
  1022. unsigned int target;
  1023. if (!cpumask_test_and_clear_cpu(cpu, &dt->cpu))
  1024. return 0;
  1025. target = cpumask_any_but(cpu_online_mask, cpu);
  1026. if (target >= nr_cpu_ids)
  1027. return 0;
  1028. perf_pmu_migrate_context(&dt->pmu, cpu, target);
  1029. cpumask_set_cpu(target, &dt->cpu);
  1030. if (ccn->irq)
  1031. WARN_ON(irq_set_affinity_hint(ccn->irq, &dt->cpu) != 0);
  1032. return 0;
  1033. }
  1034. static DEFINE_IDA(arm_ccn_pmu_ida);
  1035. static int arm_ccn_pmu_init(struct arm_ccn *ccn)
  1036. {
  1037. int i;
  1038. char *name;
  1039. int err;
  1040. /* Initialize DT subsystem */
  1041. ccn->dt.base = ccn->base + CCN_REGION_SIZE;
  1042. spin_lock_init(&ccn->dt.config_lock);
  1043. writel(CCN_DT_PMOVSR_CLR__MASK, ccn->dt.base + CCN_DT_PMOVSR_CLR);
  1044. writel(CCN_DT_CTL__DT_EN, ccn->dt.base + CCN_DT_CTL);
  1045. writel(CCN_DT_PMCR__OVFL_INTR_EN | CCN_DT_PMCR__PMU_EN,
  1046. ccn->dt.base + CCN_DT_PMCR);
  1047. writel(0x1, ccn->dt.base + CCN_DT_PMSR_CLR);
  1048. for (i = 0; i < ccn->num_xps; i++) {
  1049. writel(0, ccn->xp[i].base + CCN_XP_DT_CONFIG);
  1050. writel((CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
  1051. CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(0)) |
  1052. (CCN_XP_DT_CONTROL__WP_ARM_SEL__ALWAYS <<
  1053. CCN_XP_DT_CONTROL__WP_ARM_SEL__SHIFT(1)) |
  1054. CCN_XP_DT_CONTROL__DT_ENABLE,
  1055. ccn->xp[i].base + CCN_XP_DT_CONTROL);
  1056. }
  1057. ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].l = ~0;
  1058. ccn->dt.cmp_mask[CCN_IDX_MASK_ANY].h = ~0;
  1059. ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].l = 0;
  1060. ccn->dt.cmp_mask[CCN_IDX_MASK_EXACT].h = 0;
  1061. ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].l = ~0;
  1062. ccn->dt.cmp_mask[CCN_IDX_MASK_ORDER].h = ~(0x1 << 15);
  1063. ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].l = ~0;
  1064. ccn->dt.cmp_mask[CCN_IDX_MASK_OPCODE].h = ~(0x1f << 9);
  1065. /* Get a convenient /sys/event_source/devices/ name */
  1066. ccn->dt.id = ida_simple_get(&arm_ccn_pmu_ida, 0, 0, GFP_KERNEL);
  1067. if (ccn->dt.id == 0) {
  1068. name = "ccn";
  1069. } else {
  1070. int len = snprintf(NULL, 0, "ccn_%d", ccn->dt.id);
  1071. name = devm_kzalloc(ccn->dev, len + 1, GFP_KERNEL);
  1072. if (!name) {
  1073. err = -ENOMEM;
  1074. goto error_choose_name;
  1075. }
  1076. snprintf(name, len + 1, "ccn_%d", ccn->dt.id);
  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_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_nodes,
  1285. GFP_KERNEL);
  1286. ccn->xp = devm_kzalloc(ccn->dev, sizeof(*ccn->node) * ccn->num_xps,
  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-504", },
  1303. {},
  1304. };
  1305. static struct platform_driver arm_ccn_driver = {
  1306. .driver = {
  1307. .name = "arm-ccn",
  1308. .of_match_table = arm_ccn_match,
  1309. },
  1310. .probe = arm_ccn_probe,
  1311. .remove = arm_ccn_remove,
  1312. };
  1313. static int __init arm_ccn_init(void)
  1314. {
  1315. int i, ret;
  1316. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CCN_ONLINE,
  1317. "AP_PERF_ARM_CCN_ONLINE", NULL,
  1318. arm_ccn_pmu_offline_cpu);
  1319. if (ret)
  1320. return ret;
  1321. for (i = 0; i < ARRAY_SIZE(arm_ccn_pmu_events); i++)
  1322. arm_ccn_pmu_events_attrs[i] = &arm_ccn_pmu_events[i].attr.attr;
  1323. ret = platform_driver_register(&arm_ccn_driver);
  1324. if (ret)
  1325. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
  1326. return ret;
  1327. }
  1328. static void __exit arm_ccn_exit(void)
  1329. {
  1330. platform_driver_unregister(&arm_ccn_driver);
  1331. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CCN_ONLINE);
  1332. }
  1333. module_init(arm_ccn_init);
  1334. module_exit(arm_ccn_exit);
  1335. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  1336. MODULE_LICENSE("GPL");