arm-ni.c 20 KB

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