arm-ni.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2022-2024 Arm Limited
  3. // NI-700 Network-on-Chip PMU driver
  4. #include <linux/acpi.h>
  5. #include <linux/bitfield.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/io-64-nonatomic-lo-hi.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/perf_event.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. /* Common registers */
  16. #define NI_NODE_TYPE 0x000
  17. #define NI_NODE_TYPE_NODE_ID GENMASK(31, 16)
  18. #define NI_NODE_TYPE_NODE_TYPE GENMASK(15, 0)
  19. #define NI_CHILD_NODE_INFO 0x004
  20. #define NI_CHILD_PTR(n) (0x008 + (n) * 4)
  21. #define NI_NUM_SUB_FEATURES 0x100
  22. #define NI_SUB_FEATURE_TYPE(n) (0x108 + (n) * 8)
  23. #define NI_SUB_FEATURE_PTR(n) (0x10c + (n) * 8)
  24. #define NI_SUB_FEATURE_TYPE_FCU 0x2
  25. #define NI700_PMUSELA 0x00c
  26. /* Config node */
  27. #define NI_PERIPHERAL_ID0 0xfe0
  28. #define NI_PIDR0_PART_7_0 GENMASK(7, 0)
  29. #define NI_PERIPHERAL_ID1 0xfe4
  30. #define NI_PIDR1_PART_11_8 GENMASK(3, 0)
  31. #define NI_PERIPHERAL_ID2 0xfe8
  32. #define NI_PIDR2_VERSION GENMASK(7, 4)
  33. /* PMU node */
  34. #define NI700_PMEVCNTR(n) (0x008 + (n) * 8)
  35. #define NI700_PMCCNTR_L 0x0f8
  36. #define NI_PMEVCNTR(n) (0x200 + (n) * 8)
  37. #define NI_PMCCNTR_L 0x2f8
  38. #define NI_PMEVTYPER(n) (0x400 + (n) * 4)
  39. #define NI_PMEVTYPER_NODE_TYPE GENMASK(12, 9)
  40. #define NI_PMEVTYPER_NODE_ID GENMASK(8, 0)
  41. #define NI_PMCNTENSET 0xc00
  42. #define NI_PMCNTENCLR 0xc20
  43. #define NI_PMINTENSET 0xc40
  44. #define NI_PMINTENCLR 0xc60
  45. #define NI_PMOVSCLR 0xc80
  46. #define NI_PMOVSSET 0xcc0
  47. #define NI_PMCFGR 0xe00
  48. #define NI_PMCR 0xe04
  49. #define NI_PMCR_RESET_CCNT BIT(2)
  50. #define NI_PMCR_RESET_EVCNT BIT(1)
  51. #define NI_PMCR_ENABLE BIT(0)
  52. #define NI_NUM_COUNTERS 8
  53. #define NI_CCNT_IDX 31
  54. /* Event attributes */
  55. #define NI_CONFIG_TYPE GENMASK_ULL(15, 0)
  56. #define NI_CONFIG_NODEID GENMASK_ULL(31, 16)
  57. #define NI_CONFIG_EVENTID GENMASK_ULL(47, 32)
  58. #define NI_EVENT_TYPE(event) FIELD_GET(NI_CONFIG_TYPE, (event)->attr.config)
  59. #define NI_EVENT_NODEID(event) FIELD_GET(NI_CONFIG_NODEID, (event)->attr.config)
  60. #define NI_EVENT_EVENTID(event) FIELD_GET(NI_CONFIG_EVENTID, (event)->attr.config)
  61. enum ni_part {
  62. PART_NI_700 = 0x43b,
  63. PART_NI_710AE = 0x43d,
  64. PART_NOC_S3 = 0x43f,
  65. PART_SI_L1 = 0x455,
  66. };
  67. enum ni_node_type {
  68. NI_GLOBAL,
  69. NI_VOLTAGE,
  70. NI_POWER,
  71. NI_CLOCK,
  72. NI_ASNI,
  73. NI_AMNI,
  74. NI_PMU,
  75. NI_HSNI,
  76. NI_HMNI,
  77. NI_PMNI,
  78. NI_TSNI,
  79. NI_TMNI,
  80. NI_CMNI = 0x0e,
  81. NI_MCN = 0x63,
  82. };
  83. struct arm_ni_node {
  84. void __iomem *base;
  85. enum ni_node_type type;
  86. u16 id;
  87. u32 num_components;
  88. };
  89. struct arm_ni_unit {
  90. void __iomem *pmusela;
  91. enum ni_node_type type;
  92. u16 id;
  93. bool ns;
  94. union {
  95. __le64 pmusel;
  96. u8 event[8];
  97. };
  98. };
  99. struct arm_ni_cd {
  100. void __iomem *pmu_base;
  101. u16 id;
  102. s8 irq_friend;
  103. int num_units;
  104. int irq;
  105. struct pmu pmu;
  106. struct arm_ni_unit *units;
  107. struct perf_event *evcnt[NI_NUM_COUNTERS];
  108. struct perf_event *ccnt;
  109. };
  110. struct arm_ni {
  111. struct device *dev;
  112. void __iomem *base;
  113. enum ni_part part;
  114. int id;
  115. int cpu;
  116. int num_cds;
  117. struct hlist_node cpuhp_node;
  118. struct arm_ni_cd cds[] __counted_by(num_cds);
  119. };
  120. #define cd_to_ni(cd) container_of((cd), struct arm_ni, cds[(cd)->id])
  121. #define pmu_to_cd(p) container_of((p), struct arm_ni_cd, pmu)
  122. #define ni_for_each_cd(n, c) \
  123. for (struct arm_ni_cd *c = n->cds; c < n->cds + n->num_cds; c++) if (c->pmu_base)
  124. #define cd_for_each_unit(cd, u) \
  125. for (struct arm_ni_unit *u = cd->units; u < cd->units + cd->num_units; u++)
  126. static int arm_ni_hp_state;
  127. struct arm_ni_event_attr {
  128. struct device_attribute attr;
  129. enum ni_node_type type;
  130. };
  131. #define NI_EVENT_ATTR(_name, _type) \
  132. (&((struct arm_ni_event_attr[]) {{ \
  133. .attr = __ATTR(_name, 0444, arm_ni_event_show, NULL), \
  134. .type = _type, \
  135. }})[0].attr.attr)
  136. static ssize_t arm_ni_event_show(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. struct arm_ni_event_attr *eattr = container_of(attr, typeof(*eattr), attr);
  140. if (eattr->type == NI_PMU)
  141. return sysfs_emit(buf, "type=0x%x\n", eattr->type);
  142. return sysfs_emit(buf, "type=0x%x,eventid=?,nodeid=?\n", eattr->type);
  143. }
  144. static umode_t arm_ni_event_attr_is_visible(struct kobject *kobj,
  145. struct attribute *attr, int unused)
  146. {
  147. struct device *dev = kobj_to_dev(kobj);
  148. struct arm_ni_cd *cd = pmu_to_cd(dev_get_drvdata(dev));
  149. struct arm_ni_event_attr *eattr;
  150. eattr = container_of(attr, typeof(*eattr), attr.attr);
  151. cd_for_each_unit(cd, unit) {
  152. if (unit->type == eattr->type && unit->ns)
  153. return attr->mode;
  154. }
  155. return 0;
  156. }
  157. static struct attribute *arm_ni_event_attrs[] = {
  158. NI_EVENT_ATTR(asni, NI_ASNI),
  159. NI_EVENT_ATTR(amni, NI_AMNI),
  160. NI_EVENT_ATTR(cycles, NI_PMU),
  161. NI_EVENT_ATTR(hsni, NI_HSNI),
  162. NI_EVENT_ATTR(hmni, NI_HMNI),
  163. NI_EVENT_ATTR(pmni, NI_PMNI),
  164. NI_EVENT_ATTR(tsni, NI_TSNI),
  165. NI_EVENT_ATTR(tmni, NI_TMNI),
  166. NI_EVENT_ATTR(cmni, NI_CMNI),
  167. NULL
  168. };
  169. static const struct attribute_group arm_ni_event_attrs_group = {
  170. .name = "events",
  171. .attrs = arm_ni_event_attrs,
  172. .is_visible = arm_ni_event_attr_is_visible,
  173. };
  174. struct arm_ni_format_attr {
  175. struct device_attribute attr;
  176. u64 field;
  177. };
  178. #define NI_FORMAT_ATTR(_name, _fld) \
  179. (&((struct arm_ni_format_attr[]) {{ \
  180. .attr = __ATTR(_name, 0444, arm_ni_format_show, NULL), \
  181. .field = _fld, \
  182. }})[0].attr.attr)
  183. static ssize_t arm_ni_format_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct arm_ni_format_attr *fmt = container_of(attr, typeof(*fmt), attr);
  187. return sysfs_emit(buf, "config:%*pbl\n", 64, &fmt->field);
  188. }
  189. static struct attribute *arm_ni_format_attrs[] = {
  190. NI_FORMAT_ATTR(type, NI_CONFIG_TYPE),
  191. NI_FORMAT_ATTR(nodeid, NI_CONFIG_NODEID),
  192. NI_FORMAT_ATTR(eventid, NI_CONFIG_EVENTID),
  193. NULL
  194. };
  195. static const struct attribute_group arm_ni_format_attrs_group = {
  196. .name = "format",
  197. .attrs = arm_ni_format_attrs,
  198. };
  199. static ssize_t arm_ni_cpumask_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct arm_ni *ni = cd_to_ni(pmu_to_cd(dev_get_drvdata(dev)));
  203. return cpumap_print_to_pagebuf(true, buf, cpumask_of(ni->cpu));
  204. }
  205. static struct device_attribute arm_ni_cpumask_attr =
  206. __ATTR(cpumask, 0444, arm_ni_cpumask_show, NULL);
  207. static ssize_t arm_ni_identifier_show(struct device *dev,
  208. struct device_attribute *attr, char *buf)
  209. {
  210. struct arm_ni *ni = cd_to_ni(pmu_to_cd(dev_get_drvdata(dev)));
  211. u32 reg = readl_relaxed(ni->base + NI_PERIPHERAL_ID2);
  212. int version = FIELD_GET(NI_PIDR2_VERSION, reg);
  213. return sysfs_emit(buf, "%03x%02x\n", ni->part, version);
  214. }
  215. static struct device_attribute arm_ni_identifier_attr =
  216. __ATTR(identifier, 0444, arm_ni_identifier_show, NULL);
  217. static struct attribute *arm_ni_other_attrs[] = {
  218. &arm_ni_cpumask_attr.attr,
  219. &arm_ni_identifier_attr.attr,
  220. NULL
  221. };
  222. static const struct attribute_group arm_ni_other_attr_group = {
  223. .attrs = arm_ni_other_attrs,
  224. };
  225. static const struct attribute_group *arm_ni_attr_groups[] = {
  226. &arm_ni_event_attrs_group,
  227. &arm_ni_format_attrs_group,
  228. &arm_ni_other_attr_group,
  229. NULL
  230. };
  231. static void arm_ni_pmu_enable(struct pmu *pmu)
  232. {
  233. writel_relaxed(NI_PMCR_ENABLE, pmu_to_cd(pmu)->pmu_base + NI_PMCR);
  234. }
  235. static void arm_ni_pmu_disable(struct pmu *pmu)
  236. {
  237. writel_relaxed(0, pmu_to_cd(pmu)->pmu_base + NI_PMCR);
  238. }
  239. struct arm_ni_val {
  240. unsigned int evcnt;
  241. unsigned int ccnt;
  242. };
  243. static bool arm_ni_val_count_event(struct perf_event *evt, struct arm_ni_val *val)
  244. {
  245. if (is_software_event(evt))
  246. return true;
  247. if (NI_EVENT_TYPE(evt) == NI_PMU) {
  248. val->ccnt++;
  249. return val->ccnt <= 1;
  250. }
  251. val->evcnt++;
  252. return val->evcnt <= NI_NUM_COUNTERS;
  253. }
  254. static int arm_ni_validate_group(struct perf_event *event)
  255. {
  256. struct perf_event *sibling, *leader = event->group_leader;
  257. struct arm_ni_val val = { 0 };
  258. if (leader == event)
  259. return 0;
  260. arm_ni_val_count_event(event, &val);
  261. if (!arm_ni_val_count_event(leader, &val))
  262. return -EINVAL;
  263. for_each_sibling_event(sibling, leader) {
  264. if (!arm_ni_val_count_event(sibling, &val))
  265. return -EINVAL;
  266. }
  267. return 0;
  268. }
  269. static bool arm_ni_is_7xx(const struct arm_ni *ni)
  270. {
  271. return ni->part == PART_NI_700 || ni->part == PART_NI_710AE;
  272. }
  273. static int arm_ni_event_init(struct perf_event *event)
  274. {
  275. struct arm_ni_cd *cd = pmu_to_cd(event->pmu);
  276. struct arm_ni *ni;
  277. if (event->attr.type != event->pmu->type)
  278. return -ENOENT;
  279. if (is_sampling_event(event))
  280. return -EINVAL;
  281. ni = cd_to_ni(cd);
  282. event->cpu = ni->cpu;
  283. event->hw.flags = arm_ni_is_7xx(ni);
  284. if (NI_EVENT_TYPE(event) == NI_PMU)
  285. return arm_ni_validate_group(event);
  286. cd_for_each_unit(cd, unit) {
  287. if (unit->type == NI_EVENT_TYPE(event) &&
  288. unit->id == NI_EVENT_NODEID(event) && unit->ns) {
  289. event->hw.config_base = (unsigned long)unit;
  290. return arm_ni_validate_group(event);
  291. }
  292. }
  293. return -EINVAL;
  294. }
  295. static u64 arm_ni_read_ccnt(void __iomem *pmccntr)
  296. {
  297. u64 l, u_old, u_new;
  298. int retries = 3; /* 1st time unlucky, 2nd improbable, 3rd just broken */
  299. u_new = readl_relaxed(pmccntr + 4);
  300. do {
  301. u_old = u_new;
  302. l = readl_relaxed(pmccntr);
  303. u_new = readl_relaxed(pmccntr + 4);
  304. } while (u_new != u_old && --retries);
  305. WARN_ON(!retries);
  306. return (u_new << 32) | l;
  307. }
  308. static void arm_ni_event_read(struct perf_event *event)
  309. {
  310. struct hw_perf_event *hw = &event->hw;
  311. u64 count, prev;
  312. bool ccnt = hw->idx == NI_CCNT_IDX;
  313. do {
  314. prev = local64_read(&hw->prev_count);
  315. if (ccnt)
  316. count = arm_ni_read_ccnt((void __iomem *)event->hw.event_base);
  317. else
  318. count = readl_relaxed((void __iomem *)event->hw.event_base);
  319. } while (local64_cmpxchg(&hw->prev_count, prev, count) != prev);
  320. count -= prev;
  321. if (!ccnt)
  322. count = (u32)count;
  323. local64_add(count, &event->count);
  324. }
  325. static void arm_ni_event_start(struct perf_event *event, int flags)
  326. {
  327. struct arm_ni_cd *cd = pmu_to_cd(event->pmu);
  328. writel_relaxed(1U << event->hw.idx, cd->pmu_base + NI_PMCNTENSET);
  329. }
  330. static void arm_ni_event_stop(struct perf_event *event, int flags)
  331. {
  332. struct arm_ni_cd *cd = pmu_to_cd(event->pmu);
  333. writel_relaxed(1U << event->hw.idx, cd->pmu_base + NI_PMCNTENCLR);
  334. if (flags & PERF_EF_UPDATE)
  335. arm_ni_event_read(event);
  336. }
  337. static void arm_ni_init_ccnt(struct hw_perf_event *hw)
  338. {
  339. local64_set(&hw->prev_count, S64_MIN);
  340. lo_hi_writeq_relaxed(S64_MIN, (void __iomem *)hw->event_base);
  341. }
  342. static void arm_ni_init_evcnt(struct hw_perf_event *hw)
  343. {
  344. local64_set(&hw->prev_count, S32_MIN);
  345. writel_relaxed(S32_MIN, (void __iomem *)hw->event_base);
  346. }
  347. static int arm_ni_event_add(struct perf_event *event, int flags)
  348. {
  349. struct arm_ni_cd *cd = pmu_to_cd(event->pmu);
  350. struct hw_perf_event *hw = &event->hw;
  351. struct arm_ni_unit *unit;
  352. enum ni_node_type type = NI_EVENT_TYPE(event);
  353. u32 reg;
  354. if (type == NI_PMU) {
  355. if (cd->ccnt)
  356. return -ENOSPC;
  357. hw->idx = NI_CCNT_IDX;
  358. hw->event_base = (unsigned long)cd->pmu_base +
  359. (hw->flags ? NI700_PMCCNTR_L : NI_PMCCNTR_L);
  360. cd->ccnt = event;
  361. arm_ni_init_ccnt(hw);
  362. } else {
  363. hw->idx = 0;
  364. while (cd->evcnt[hw->idx]) {
  365. if (++hw->idx == NI_NUM_COUNTERS)
  366. return -ENOSPC;
  367. }
  368. cd->evcnt[hw->idx] = event;
  369. unit = (void *)hw->config_base;
  370. unit->event[hw->idx] = NI_EVENT_EVENTID(event);
  371. hw->event_base = (unsigned long)cd->pmu_base +
  372. (hw->flags ? NI700_PMEVCNTR(hw->idx) : NI_PMEVCNTR(hw->idx));
  373. arm_ni_init_evcnt(hw);
  374. lo_hi_writeq_relaxed(le64_to_cpu(unit->pmusel), unit->pmusela);
  375. reg = FIELD_PREP(NI_PMEVTYPER_NODE_TYPE, type) |
  376. FIELD_PREP(NI_PMEVTYPER_NODE_ID, NI_EVENT_NODEID(event));
  377. writel_relaxed(reg, cd->pmu_base + NI_PMEVTYPER(hw->idx));
  378. }
  379. if (flags & PERF_EF_START)
  380. arm_ni_event_start(event, 0);
  381. return 0;
  382. }
  383. static void arm_ni_event_del(struct perf_event *event, int flags)
  384. {
  385. struct arm_ni_cd *cd = pmu_to_cd(event->pmu);
  386. struct hw_perf_event *hw = &event->hw;
  387. arm_ni_event_stop(event, PERF_EF_UPDATE);
  388. if (hw->idx == NI_CCNT_IDX)
  389. cd->ccnt = NULL;
  390. else
  391. cd->evcnt[hw->idx] = NULL;
  392. }
  393. static irqreturn_t arm_ni_handle_irq(int irq, void *dev_id)
  394. {
  395. struct arm_ni_cd *cd = dev_id;
  396. irqreturn_t ret = IRQ_NONE;
  397. for (;;) {
  398. u32 reg = readl_relaxed(cd->pmu_base + NI_PMOVSCLR);
  399. if (reg & (1U << NI_CCNT_IDX)) {
  400. ret = IRQ_HANDLED;
  401. if (!(WARN_ON(!cd->ccnt))) {
  402. arm_ni_event_read(cd->ccnt);
  403. arm_ni_init_ccnt(&cd->ccnt->hw);
  404. }
  405. }
  406. for (int i = 0; i < NI_NUM_COUNTERS; i++) {
  407. if (!(reg & (1U << i)))
  408. continue;
  409. ret = IRQ_HANDLED;
  410. if (!(WARN_ON(!cd->evcnt[i]))) {
  411. arm_ni_event_read(cd->evcnt[i]);
  412. arm_ni_init_evcnt(&cd->evcnt[i]->hw);
  413. }
  414. }
  415. writel_relaxed(reg, cd->pmu_base + NI_PMOVSCLR);
  416. if (!cd->irq_friend)
  417. return ret;
  418. cd += cd->irq_friend;
  419. }
  420. }
  421. static void __iomem *arm_ni_get_pmusel(struct arm_ni *ni, void __iomem *unit_base)
  422. {
  423. u32 type, ptr, num;
  424. if (arm_ni_is_7xx(ni))
  425. return unit_base + NI700_PMUSELA;
  426. num = readl_relaxed(unit_base + NI_NUM_SUB_FEATURES);
  427. for (int i = 0; i < num; i++) {
  428. type = readl_relaxed(unit_base + NI_SUB_FEATURE_TYPE(i));
  429. if (type != NI_SUB_FEATURE_TYPE_FCU)
  430. continue;
  431. ptr = readl_relaxed(unit_base + NI_SUB_FEATURE_PTR(i));
  432. return ni->base + ptr;
  433. }
  434. /* Should be impossible */
  435. return NULL;
  436. }
  437. static int arm_ni_init_cd(struct arm_ni *ni, struct arm_ni_node *node, u64 res_start)
  438. {
  439. struct arm_ni_cd *cd = ni->cds + node->id;
  440. const char *name;
  441. cd->id = node->id;
  442. cd->num_units = node->num_components;
  443. cd->units = devm_kcalloc(ni->dev, cd->num_units, sizeof(*(cd->units)), GFP_KERNEL);
  444. if (!cd->units)
  445. return -ENOMEM;
  446. for (int i = 0; i < cd->num_units; i++) {
  447. u32 reg = readl_relaxed(node->base + NI_CHILD_PTR(i));
  448. void __iomem *unit_base = ni->base + reg;
  449. struct arm_ni_unit *unit = cd->units + i;
  450. reg = readl_relaxed(unit_base + NI_NODE_TYPE);
  451. unit->type = FIELD_GET(NI_NODE_TYPE_NODE_TYPE, reg);
  452. unit->id = FIELD_GET(NI_NODE_TYPE_NODE_ID, reg);
  453. switch (unit->type) {
  454. case NI_PMU:
  455. reg = readl_relaxed(unit_base + NI_PMCFGR);
  456. if (!reg) {
  457. dev_info(ni->dev, "No access to PMU %d\n", cd->id);
  458. devm_kfree(ni->dev, cd->units);
  459. return 0;
  460. }
  461. unit->ns = true;
  462. cd->pmu_base = unit_base;
  463. break;
  464. case NI_ASNI:
  465. case NI_AMNI:
  466. case NI_HSNI:
  467. case NI_HMNI:
  468. case NI_PMNI:
  469. case NI_TSNI:
  470. case NI_TMNI:
  471. case NI_CMNI:
  472. unit->pmusela = arm_ni_get_pmusel(ni, unit_base);
  473. writel_relaxed(1, unit->pmusela);
  474. if (readl_relaxed(unit->pmusela) != 1)
  475. dev_info(ni->dev, "No access to node 0x%04x%04x\n", unit->id, unit->type);
  476. else
  477. unit->ns = true;
  478. break;
  479. case NI_MCN:
  480. break;
  481. default:
  482. /*
  483. * e.g. FMU - thankfully bits 3:2 of FMU_ERR_FR0 are RES0 so
  484. * can't alias any of the leaf node types we're looking for.
  485. */
  486. dev_dbg(ni->dev, "Mystery node 0x%04x%04x\n", unit->id, unit->type);
  487. break;
  488. }
  489. }
  490. res_start += cd->pmu_base - ni->base;
  491. if (!devm_request_mem_region(ni->dev, res_start, SZ_4K, dev_name(ni->dev))) {
  492. dev_err(ni->dev, "Failed to request PMU region 0x%llx\n", res_start);
  493. return -EBUSY;
  494. }
  495. writel_relaxed(NI_PMCR_RESET_CCNT | NI_PMCR_RESET_EVCNT,
  496. cd->pmu_base + NI_PMCR);
  497. writel_relaxed(U32_MAX, cd->pmu_base + NI_PMCNTENCLR);
  498. writel_relaxed(U32_MAX, cd->pmu_base + NI_PMOVSCLR);
  499. cd->irq = platform_get_irq(to_platform_device(ni->dev), cd->id);
  500. if (cd->irq < 0)
  501. return cd->irq;
  502. cd->pmu = (struct pmu) {
  503. .module = THIS_MODULE,
  504. .parent = ni->dev,
  505. .attr_groups = arm_ni_attr_groups,
  506. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  507. .task_ctx_nr = perf_invalid_context,
  508. .pmu_enable = arm_ni_pmu_enable,
  509. .pmu_disable = arm_ni_pmu_disable,
  510. .event_init = arm_ni_event_init,
  511. .add = arm_ni_event_add,
  512. .del = arm_ni_event_del,
  513. .start = arm_ni_event_start,
  514. .stop = arm_ni_event_stop,
  515. .read = arm_ni_event_read,
  516. };
  517. name = devm_kasprintf(ni->dev, GFP_KERNEL, "arm_ni_%d_cd_%d", ni->id, cd->id);
  518. if (!name)
  519. return -ENOMEM;
  520. return perf_pmu_register(&cd->pmu, name, -1);
  521. }
  522. static void arm_ni_remove(struct platform_device *pdev)
  523. {
  524. struct arm_ni *ni = platform_get_drvdata(pdev);
  525. ni_for_each_cd(ni, cd) {
  526. writel_relaxed(0, cd->pmu_base + NI_PMCR);
  527. writel_relaxed(U32_MAX, cd->pmu_base + NI_PMINTENCLR);
  528. perf_pmu_unregister(&cd->pmu);
  529. }
  530. cpuhp_state_remove_instance_nocalls(arm_ni_hp_state, &ni->cpuhp_node);
  531. }
  532. static void arm_ni_probe_domain(void __iomem *base, struct arm_ni_node *node)
  533. {
  534. u32 reg = readl_relaxed(base + NI_NODE_TYPE);
  535. node->base = base;
  536. node->type = FIELD_GET(NI_NODE_TYPE_NODE_TYPE, reg);
  537. node->id = FIELD_GET(NI_NODE_TYPE_NODE_ID, reg);
  538. node->num_components = readl_relaxed(base + NI_CHILD_NODE_INFO);
  539. }
  540. static int arm_ni_init_irqs(struct arm_ni *ni)
  541. {
  542. int err;
  543. ni_for_each_cd(ni, cd) {
  544. for (struct arm_ni_cd *prev = cd; prev-- > ni->cds; ) {
  545. if (prev->irq == cd->irq) {
  546. prev->irq_friend = cd - prev;
  547. goto set_inten;
  548. }
  549. }
  550. err = devm_request_irq(ni->dev, cd->irq, arm_ni_handle_irq,
  551. IRQF_NOBALANCING | IRQF_NO_THREAD | IRQF_NO_AUTOEN,
  552. dev_name(ni->dev), cd);
  553. if (err)
  554. return err;
  555. irq_set_affinity(cd->irq, cpumask_of(ni->cpu));
  556. set_inten:
  557. writel_relaxed(U32_MAX, cd->pmu_base + NI_PMINTENSET);
  558. }
  559. ni_for_each_cd(ni, cd)
  560. if (!cd->irq_friend)
  561. enable_irq(cd->irq);
  562. return 0;
  563. }
  564. static int arm_ni_probe(struct platform_device *pdev)
  565. {
  566. struct arm_ni_node cfg, vd, pd, cd;
  567. struct arm_ni *ni;
  568. struct resource *res;
  569. void __iomem *base;
  570. static atomic_t id;
  571. int ret, num_cds;
  572. u32 reg, part;
  573. /*
  574. * We want to map the whole configuration space for ease of discovery,
  575. * but the PMU pages are the only ones for which we can honestly claim
  576. * exclusive ownership, so we'll request them explicitly once found.
  577. */
  578. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  579. base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  580. if (!base)
  581. return -ENOMEM;
  582. arm_ni_probe_domain(base, &cfg);
  583. if (cfg.type != NI_GLOBAL)
  584. return -ENODEV;
  585. reg = readl_relaxed(cfg.base + NI_PERIPHERAL_ID0);
  586. part = FIELD_GET(NI_PIDR0_PART_7_0, reg);
  587. reg = readl_relaxed(cfg.base + NI_PERIPHERAL_ID1);
  588. part |= FIELD_GET(NI_PIDR1_PART_11_8, reg) << 8;
  589. switch (part) {
  590. case PART_NI_700:
  591. case PART_NI_710AE:
  592. case PART_NOC_S3:
  593. case PART_SI_L1:
  594. break;
  595. default:
  596. dev_WARN(&pdev->dev, "Unknown part number: 0x%03x, this may go badly\n", part);
  597. break;
  598. }
  599. num_cds = 0;
  600. for (int v = 0; v < cfg.num_components; v++) {
  601. reg = readl_relaxed(cfg.base + NI_CHILD_PTR(v));
  602. arm_ni_probe_domain(base + reg, &vd);
  603. for (int p = 0; p < vd.num_components; p++) {
  604. reg = readl_relaxed(vd.base + NI_CHILD_PTR(p));
  605. arm_ni_probe_domain(base + reg, &pd);
  606. num_cds += pd.num_components;
  607. }
  608. }
  609. ni = devm_kzalloc(&pdev->dev, struct_size(ni, cds, num_cds), GFP_KERNEL);
  610. if (!ni)
  611. return -ENOMEM;
  612. ni->dev = &pdev->dev;
  613. ni->base = base;
  614. ni->num_cds = num_cds;
  615. ni->part = part;
  616. ni->id = atomic_fetch_inc(&id);
  617. ni->cpu = cpumask_local_spread(0, dev_to_node(ni->dev));
  618. platform_set_drvdata(pdev, ni);
  619. ret = cpuhp_state_add_instance_nocalls(arm_ni_hp_state, &ni->cpuhp_node);
  620. if (ret)
  621. return ret;
  622. for (int v = 0; v < cfg.num_components; v++) {
  623. reg = readl_relaxed(cfg.base + NI_CHILD_PTR(v));
  624. arm_ni_probe_domain(base + reg, &vd);
  625. for (int p = 0; p < vd.num_components; p++) {
  626. reg = readl_relaxed(vd.base + NI_CHILD_PTR(p));
  627. arm_ni_probe_domain(base + reg, &pd);
  628. for (int c = 0; c < pd.num_components; c++) {
  629. reg = readl_relaxed(pd.base + NI_CHILD_PTR(c));
  630. arm_ni_probe_domain(base + reg, &cd);
  631. ret = arm_ni_init_cd(ni, &cd, res->start);
  632. if (ret) {
  633. ni->cds[cd.id].pmu_base = NULL;
  634. arm_ni_remove(pdev);
  635. return ret;
  636. }
  637. }
  638. }
  639. }
  640. ret = arm_ni_init_irqs(ni);
  641. if (ret)
  642. arm_ni_remove(pdev);
  643. return ret;
  644. }
  645. #ifdef CONFIG_OF
  646. static const struct of_device_id arm_ni_of_match[] = {
  647. { .compatible = "arm,ni-700" },
  648. {}
  649. };
  650. MODULE_DEVICE_TABLE(of, arm_ni_of_match);
  651. #endif
  652. #ifdef CONFIG_ACPI
  653. static const struct acpi_device_id arm_ni_acpi_match[] = {
  654. { "ARMHCB70" },
  655. {}
  656. };
  657. MODULE_DEVICE_TABLE(acpi, arm_ni_acpi_match);
  658. #endif
  659. static struct platform_driver arm_ni_driver = {
  660. .driver = {
  661. .name = "arm-ni",
  662. .of_match_table = of_match_ptr(arm_ni_of_match),
  663. .acpi_match_table = ACPI_PTR(arm_ni_acpi_match),
  664. .suppress_bind_attrs = true,
  665. },
  666. .probe = arm_ni_probe,
  667. .remove = arm_ni_remove,
  668. };
  669. static void arm_ni_pmu_migrate(struct arm_ni *ni, unsigned int cpu)
  670. {
  671. ni_for_each_cd(ni, cd) {
  672. perf_pmu_migrate_context(&cd->pmu, ni->cpu, cpu);
  673. irq_set_affinity(cd->irq, cpumask_of(cpu));
  674. }
  675. ni->cpu = cpu;
  676. }
  677. static int arm_ni_pmu_online_cpu(unsigned int cpu, struct hlist_node *cpuhp_node)
  678. {
  679. struct arm_ni *ni;
  680. int node;
  681. ni = hlist_entry_safe(cpuhp_node, struct arm_ni, cpuhp_node);
  682. node = dev_to_node(ni->dev);
  683. if (cpu_to_node(ni->cpu) != node && cpu_to_node(cpu) == node)
  684. arm_ni_pmu_migrate(ni, cpu);
  685. return 0;
  686. }
  687. static int arm_ni_pmu_offline_cpu(unsigned int cpu, struct hlist_node *cpuhp_node)
  688. {
  689. struct arm_ni *ni;
  690. unsigned int target;
  691. int node;
  692. ni = hlist_entry_safe(cpuhp_node, struct arm_ni, cpuhp_node);
  693. if (cpu != ni->cpu)
  694. return 0;
  695. node = dev_to_node(ni->dev);
  696. target = cpumask_any_and_but(cpumask_of_node(node), cpu_online_mask, cpu);
  697. if (target >= nr_cpu_ids)
  698. target = cpumask_any_but(cpu_online_mask, cpu);
  699. if (target < nr_cpu_ids)
  700. arm_ni_pmu_migrate(ni, target);
  701. return 0;
  702. }
  703. static int __init arm_ni_init(void)
  704. {
  705. int ret;
  706. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  707. "perf/arm/ni:online",
  708. arm_ni_pmu_online_cpu,
  709. arm_ni_pmu_offline_cpu);
  710. if (ret < 0)
  711. return ret;
  712. arm_ni_hp_state = ret;
  713. ret = platform_driver_register(&arm_ni_driver);
  714. if (ret)
  715. cpuhp_remove_multi_state(arm_ni_hp_state);
  716. return ret;
  717. }
  718. static void __exit arm_ni_exit(void)
  719. {
  720. platform_driver_unregister(&arm_ni_driver);
  721. cpuhp_remove_multi_state(arm_ni_hp_state);
  722. }
  723. module_init(arm_ni_init);
  724. module_exit(arm_ni_exit);
  725. MODULE_AUTHOR("Robin Murphy <robin.murphy@arm.com>");
  726. MODULE_DESCRIPTION("Arm NI-700 PMU driver");
  727. MODULE_LICENSE("GPL v2");