fujitsu_uncore_pmu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the Uncore PMUs in Fujitsu chips.
  4. *
  5. * See Documentation/admin-guide/perf/fujitsu_uncore_pmu.rst for more details.
  6. *
  7. * Copyright (c) 2025 Fujitsu. All rights reserved.
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/bitfield.h>
  11. #include <linux/bitops.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/list.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/perf_event.h>
  18. #include <linux/platform_device.h>
  19. /* Number of counters on each PMU */
  20. #define MAC_NUM_COUNTERS 8
  21. #define PCI_NUM_COUNTERS 8
  22. /* Mask for the event type field within perf_event_attr.config and EVTYPE reg */
  23. #define UNCORE_EVTYPE_MASK 0xFF
  24. /* Perfmon registers */
  25. #define PM_EVCNTR(__cntr) (0x000 + (__cntr) * 8)
  26. #define PM_CNTCTL(__cntr) (0x100 + (__cntr) * 8)
  27. #define PM_CNTCTL_RESET 0
  28. #define PM_EVTYPE(__cntr) (0x200 + (__cntr) * 8)
  29. #define PM_EVTYPE_EVSEL(__val) FIELD_GET(UNCORE_EVTYPE_MASK, __val)
  30. #define PM_CR 0x400
  31. #define PM_CR_RESET BIT(1)
  32. #define PM_CR_ENABLE BIT(0)
  33. #define PM_CNTENSET 0x410
  34. #define PM_CNTENSET_IDX(__cntr) BIT(__cntr)
  35. #define PM_CNTENCLR 0x418
  36. #define PM_CNTENCLR_IDX(__cntr) BIT(__cntr)
  37. #define PM_CNTENCLR_RESET 0xFF
  38. #define PM_INTENSET 0x420
  39. #define PM_INTENSET_IDX(__cntr) BIT(__cntr)
  40. #define PM_INTENCLR 0x428
  41. #define PM_INTENCLR_IDX(__cntr) BIT(__cntr)
  42. #define PM_INTENCLR_RESET 0xFF
  43. #define PM_OVSR 0x440
  44. #define PM_OVSR_OVSRCLR_RESET 0xFF
  45. enum fujitsu_uncore_pmu {
  46. FUJITSU_UNCORE_PMU_MAC = 1,
  47. FUJITSU_UNCORE_PMU_PCI = 2,
  48. };
  49. struct uncore_pmu {
  50. int num_counters;
  51. struct pmu pmu;
  52. struct hlist_node node;
  53. void __iomem *regs;
  54. struct perf_event **events;
  55. unsigned long *used_mask;
  56. int cpu;
  57. int irq;
  58. struct device *dev;
  59. };
  60. #define to_uncore_pmu(p) (container_of(p, struct uncore_pmu, pmu))
  61. static int uncore_pmu_cpuhp_state;
  62. static void fujitsu_uncore_counter_start(struct perf_event *event)
  63. {
  64. struct uncore_pmu *uncorepmu = to_uncore_pmu(event->pmu);
  65. int idx = event->hw.idx;
  66. /* Initialize the hardware counter and reset prev_count*/
  67. local64_set(&event->hw.prev_count, 0);
  68. writeq_relaxed(0, uncorepmu->regs + PM_EVCNTR(idx));
  69. /* Set the event type */
  70. writeq_relaxed(PM_EVTYPE_EVSEL(event->attr.config), uncorepmu->regs + PM_EVTYPE(idx));
  71. /* Enable interrupt generation by this counter */
  72. writeq_relaxed(PM_INTENSET_IDX(idx), uncorepmu->regs + PM_INTENSET);
  73. /* Finally, enable the counter */
  74. writeq_relaxed(PM_CNTCTL_RESET, uncorepmu->regs + PM_CNTCTL(idx));
  75. writeq_relaxed(PM_CNTENSET_IDX(idx), uncorepmu->regs + PM_CNTENSET);
  76. }
  77. static void fujitsu_uncore_counter_stop(struct perf_event *event)
  78. {
  79. struct uncore_pmu *uncorepmu = to_uncore_pmu(event->pmu);
  80. int idx = event->hw.idx;
  81. /* Disable the counter */
  82. writeq_relaxed(PM_CNTENCLR_IDX(idx), uncorepmu->regs + PM_CNTENCLR);
  83. /* Disable interrupt generation by this counter */
  84. writeq_relaxed(PM_INTENCLR_IDX(idx), uncorepmu->regs + PM_INTENCLR);
  85. }
  86. static void fujitsu_uncore_counter_update(struct perf_event *event)
  87. {
  88. struct uncore_pmu *uncorepmu = to_uncore_pmu(event->pmu);
  89. int idx = event->hw.idx;
  90. u64 prev, new;
  91. do {
  92. prev = local64_read(&event->hw.prev_count);
  93. new = readq_relaxed(uncorepmu->regs + PM_EVCNTR(idx));
  94. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  95. local64_add(new - prev, &event->count);
  96. }
  97. static inline void fujitsu_uncore_init(struct uncore_pmu *uncorepmu)
  98. {
  99. int i;
  100. writeq_relaxed(PM_CR_RESET, uncorepmu->regs + PM_CR);
  101. writeq_relaxed(PM_CNTENCLR_RESET, uncorepmu->regs + PM_CNTENCLR);
  102. writeq_relaxed(PM_INTENCLR_RESET, uncorepmu->regs + PM_INTENCLR);
  103. writeq_relaxed(PM_OVSR_OVSRCLR_RESET, uncorepmu->regs + PM_OVSR);
  104. for (i = 0; i < uncorepmu->num_counters; ++i) {
  105. writeq_relaxed(PM_CNTCTL_RESET, uncorepmu->regs + PM_CNTCTL(i));
  106. writeq_relaxed(PM_EVTYPE_EVSEL(0), uncorepmu->regs + PM_EVTYPE(i));
  107. }
  108. writeq_relaxed(PM_CR_ENABLE, uncorepmu->regs + PM_CR);
  109. }
  110. static irqreturn_t fujitsu_uncore_handle_irq(int irq_num, void *data)
  111. {
  112. struct uncore_pmu *uncorepmu = data;
  113. /* Read the overflow status register */
  114. long status = readq_relaxed(uncorepmu->regs + PM_OVSR);
  115. int idx;
  116. if (status == 0)
  117. return IRQ_NONE;
  118. /* Clear the bits we read on the overflow status register */
  119. writeq_relaxed(status, uncorepmu->regs + PM_OVSR);
  120. for_each_set_bit(idx, &status, uncorepmu->num_counters) {
  121. struct perf_event *event;
  122. event = uncorepmu->events[idx];
  123. if (!event)
  124. continue;
  125. fujitsu_uncore_counter_update(event);
  126. }
  127. return IRQ_HANDLED;
  128. }
  129. static void fujitsu_uncore_pmu_enable(struct pmu *pmu)
  130. {
  131. writeq_relaxed(PM_CR_ENABLE, to_uncore_pmu(pmu)->regs + PM_CR);
  132. }
  133. static void fujitsu_uncore_pmu_disable(struct pmu *pmu)
  134. {
  135. writeq_relaxed(0, to_uncore_pmu(pmu)->regs + PM_CR);
  136. }
  137. static bool fujitsu_uncore_validate_event_group(struct perf_event *event)
  138. {
  139. struct uncore_pmu *uncorepmu = to_uncore_pmu(event->pmu);
  140. struct perf_event *leader = event->group_leader;
  141. struct perf_event *sibling;
  142. int counters = 1;
  143. if (leader == event)
  144. return true;
  145. if (leader->pmu == event->pmu)
  146. counters++;
  147. for_each_sibling_event(sibling, leader) {
  148. if (sibling->pmu == event->pmu)
  149. counters++;
  150. }
  151. /*
  152. * If the group requires more counters than the HW has, it
  153. * cannot ever be scheduled.
  154. */
  155. return counters <= uncorepmu->num_counters;
  156. }
  157. static int fujitsu_uncore_event_init(struct perf_event *event)
  158. {
  159. struct uncore_pmu *uncorepmu = to_uncore_pmu(event->pmu);
  160. struct hw_perf_event *hwc = &event->hw;
  161. /* Is the event for this PMU? */
  162. if (event->attr.type != event->pmu->type)
  163. return -ENOENT;
  164. /*
  165. * Sampling not supported since these events are not
  166. * core-attributable.
  167. */
  168. if (is_sampling_event(event))
  169. return -EINVAL;
  170. /*
  171. * Task mode not available, we run the counters as socket counters,
  172. * not attributable to any CPU and therefore cannot attribute per-task.
  173. */
  174. if (event->cpu < 0)
  175. return -EINVAL;
  176. /* Validate the group */
  177. if (!fujitsu_uncore_validate_event_group(event))
  178. return -EINVAL;
  179. hwc->idx = -1;
  180. event->cpu = uncorepmu->cpu;
  181. return 0;
  182. }
  183. static void fujitsu_uncore_event_start(struct perf_event *event, int flags)
  184. {
  185. struct hw_perf_event *hwc = &event->hw;
  186. hwc->state = 0;
  187. fujitsu_uncore_counter_start(event);
  188. }
  189. static void fujitsu_uncore_event_stop(struct perf_event *event, int flags)
  190. {
  191. struct hw_perf_event *hwc = &event->hw;
  192. if (hwc->state & PERF_HES_STOPPED)
  193. return;
  194. fujitsu_uncore_counter_stop(event);
  195. if (flags & PERF_EF_UPDATE)
  196. fujitsu_uncore_counter_update(event);
  197. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  198. }
  199. static int fujitsu_uncore_event_add(struct perf_event *event, int flags)
  200. {
  201. struct uncore_pmu *uncorepmu = to_uncore_pmu(event->pmu);
  202. struct hw_perf_event *hwc = &event->hw;
  203. int idx;
  204. /* Try to allocate a counter. */
  205. idx = bitmap_find_free_region(uncorepmu->used_mask, uncorepmu->num_counters, 0);
  206. if (idx < 0)
  207. /* The counters are all in use. */
  208. return -EAGAIN;
  209. hwc->idx = idx;
  210. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  211. uncorepmu->events[idx] = event;
  212. if (flags & PERF_EF_START)
  213. fujitsu_uncore_event_start(event, 0);
  214. /* Propagate changes to the userspace mapping. */
  215. perf_event_update_userpage(event);
  216. return 0;
  217. }
  218. static void fujitsu_uncore_event_del(struct perf_event *event, int flags)
  219. {
  220. struct uncore_pmu *uncorepmu = to_uncore_pmu(event->pmu);
  221. struct hw_perf_event *hwc = &event->hw;
  222. /* Stop and clean up */
  223. fujitsu_uncore_event_stop(event, flags | PERF_EF_UPDATE);
  224. uncorepmu->events[hwc->idx] = NULL;
  225. bitmap_release_region(uncorepmu->used_mask, hwc->idx, 0);
  226. /* Propagate changes to the userspace mapping. */
  227. perf_event_update_userpage(event);
  228. }
  229. static void fujitsu_uncore_event_read(struct perf_event *event)
  230. {
  231. fujitsu_uncore_counter_update(event);
  232. }
  233. #define UNCORE_PMU_FORMAT_ATTR(_name, _config) \
  234. (&((struct dev_ext_attribute[]) { \
  235. { .attr = __ATTR(_name, 0444, device_show_string, NULL), \
  236. .var = (void *)_config, } \
  237. })[0].attr.attr)
  238. static struct attribute *fujitsu_uncore_pmu_formats[] = {
  239. UNCORE_PMU_FORMAT_ATTR(event, "config:0-7"),
  240. NULL
  241. };
  242. static const struct attribute_group fujitsu_uncore_pmu_format_group = {
  243. .name = "format",
  244. .attrs = fujitsu_uncore_pmu_formats,
  245. };
  246. static ssize_t fujitsu_uncore_pmu_event_show(struct device *dev,
  247. struct device_attribute *attr, char *page)
  248. {
  249. struct perf_pmu_events_attr *pmu_attr;
  250. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  251. return sysfs_emit(page, "event=0x%02llx\n", pmu_attr->id);
  252. }
  253. #define MAC_EVENT_ATTR(_name, _id) \
  254. PMU_EVENT_ATTR_ID(_name, fujitsu_uncore_pmu_event_show, _id)
  255. static struct attribute *fujitsu_uncore_mac_pmu_events[] = {
  256. MAC_EVENT_ATTR(cycles, 0x00),
  257. MAC_EVENT_ATTR(read-count, 0x10),
  258. MAC_EVENT_ATTR(read-count-request, 0x11),
  259. MAC_EVENT_ATTR(read-count-return, 0x12),
  260. MAC_EVENT_ATTR(read-count-request-pftgt, 0x13),
  261. MAC_EVENT_ATTR(read-count-request-normal, 0x14),
  262. MAC_EVENT_ATTR(read-count-return-pftgt-hit, 0x15),
  263. MAC_EVENT_ATTR(read-count-return-pftgt-miss, 0x16),
  264. MAC_EVENT_ATTR(read-wait, 0x17),
  265. MAC_EVENT_ATTR(write-count, 0x20),
  266. MAC_EVENT_ATTR(write-count-write, 0x21),
  267. MAC_EVENT_ATTR(write-count-pwrite, 0x22),
  268. MAC_EVENT_ATTR(memory-read-count, 0x40),
  269. MAC_EVENT_ATTR(memory-write-count, 0x50),
  270. MAC_EVENT_ATTR(memory-pwrite-count, 0x60),
  271. MAC_EVENT_ATTR(ea-mac, 0x80),
  272. MAC_EVENT_ATTR(ea-memory, 0x90),
  273. MAC_EVENT_ATTR(ea-memory-mac-write, 0x92),
  274. MAC_EVENT_ATTR(ea-ha, 0xa0),
  275. NULL
  276. };
  277. #define PCI_EVENT_ATTR(_name, _id) \
  278. PMU_EVENT_ATTR_ID(_name, fujitsu_uncore_pmu_event_show, _id)
  279. static struct attribute *fujitsu_uncore_pci_pmu_events[] = {
  280. PCI_EVENT_ATTR(pci-port0-cycles, 0x00),
  281. PCI_EVENT_ATTR(pci-port0-read-count, 0x10),
  282. PCI_EVENT_ATTR(pci-port0-read-count-bus, 0x14),
  283. PCI_EVENT_ATTR(pci-port0-write-count, 0x20),
  284. PCI_EVENT_ATTR(pci-port0-write-count-bus, 0x24),
  285. PCI_EVENT_ATTR(pci-port1-cycles, 0x40),
  286. PCI_EVENT_ATTR(pci-port1-read-count, 0x50),
  287. PCI_EVENT_ATTR(pci-port1-read-count-bus, 0x54),
  288. PCI_EVENT_ATTR(pci-port1-write-count, 0x60),
  289. PCI_EVENT_ATTR(pci-port1-write-count-bus, 0x64),
  290. PCI_EVENT_ATTR(ea-pci, 0x80),
  291. NULL
  292. };
  293. static const struct attribute_group fujitsu_uncore_mac_pmu_events_group = {
  294. .name = "events",
  295. .attrs = fujitsu_uncore_mac_pmu_events,
  296. };
  297. static const struct attribute_group fujitsu_uncore_pci_pmu_events_group = {
  298. .name = "events",
  299. .attrs = fujitsu_uncore_pci_pmu_events,
  300. };
  301. static ssize_t cpumask_show(struct device *dev,
  302. struct device_attribute *attr, char *buf)
  303. {
  304. struct uncore_pmu *uncorepmu = to_uncore_pmu(dev_get_drvdata(dev));
  305. return cpumap_print_to_pagebuf(true, buf, cpumask_of(uncorepmu->cpu));
  306. }
  307. static DEVICE_ATTR_RO(cpumask);
  308. static struct attribute *fujitsu_uncore_pmu_cpumask_attrs[] = {
  309. &dev_attr_cpumask.attr,
  310. NULL
  311. };
  312. static const struct attribute_group fujitsu_uncore_pmu_cpumask_attr_group = {
  313. .attrs = fujitsu_uncore_pmu_cpumask_attrs,
  314. };
  315. static const struct attribute_group *fujitsu_uncore_mac_pmu_attr_grps[] = {
  316. &fujitsu_uncore_pmu_format_group,
  317. &fujitsu_uncore_mac_pmu_events_group,
  318. &fujitsu_uncore_pmu_cpumask_attr_group,
  319. NULL
  320. };
  321. static const struct attribute_group *fujitsu_uncore_pci_pmu_attr_grps[] = {
  322. &fujitsu_uncore_pmu_format_group,
  323. &fujitsu_uncore_pci_pmu_events_group,
  324. &fujitsu_uncore_pmu_cpumask_attr_group,
  325. NULL
  326. };
  327. static void fujitsu_uncore_pmu_migrate(struct uncore_pmu *uncorepmu, unsigned int cpu)
  328. {
  329. perf_pmu_migrate_context(&uncorepmu->pmu, uncorepmu->cpu, cpu);
  330. irq_set_affinity(uncorepmu->irq, cpumask_of(cpu));
  331. uncorepmu->cpu = cpu;
  332. }
  333. static int fujitsu_uncore_pmu_online_cpu(unsigned int cpu, struct hlist_node *cpuhp_node)
  334. {
  335. struct uncore_pmu *uncorepmu;
  336. int node;
  337. uncorepmu = hlist_entry_safe(cpuhp_node, struct uncore_pmu, node);
  338. node = dev_to_node(uncorepmu->dev);
  339. if (cpu_to_node(uncorepmu->cpu) != node && cpu_to_node(cpu) == node)
  340. fujitsu_uncore_pmu_migrate(uncorepmu, cpu);
  341. return 0;
  342. }
  343. static int fujitsu_uncore_pmu_offline_cpu(unsigned int cpu, struct hlist_node *cpuhp_node)
  344. {
  345. struct uncore_pmu *uncorepmu;
  346. unsigned int target;
  347. int node;
  348. uncorepmu = hlist_entry_safe(cpuhp_node, struct uncore_pmu, node);
  349. if (cpu != uncorepmu->cpu)
  350. return 0;
  351. node = dev_to_node(uncorepmu->dev);
  352. target = cpumask_any_and_but(cpumask_of_node(node), cpu_online_mask, cpu);
  353. if (target >= nr_cpu_ids)
  354. target = cpumask_any_but(cpu_online_mask, cpu);
  355. if (target < nr_cpu_ids)
  356. fujitsu_uncore_pmu_migrate(uncorepmu, target);
  357. return 0;
  358. }
  359. static int fujitsu_uncore_pmu_probe(struct platform_device *pdev)
  360. {
  361. struct device *dev = &pdev->dev;
  362. unsigned long device_type = (unsigned long)device_get_match_data(dev);
  363. const struct attribute_group **attr_groups;
  364. struct uncore_pmu *uncorepmu;
  365. struct resource *memrc;
  366. size_t alloc_size;
  367. char *name;
  368. int ret;
  369. int irq;
  370. u64 uid;
  371. ret = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &uid);
  372. if (ret)
  373. return dev_err_probe(dev, ret, "unable to read ACPI uid\n");
  374. uncorepmu = devm_kzalloc(dev, sizeof(*uncorepmu), GFP_KERNEL);
  375. if (!uncorepmu)
  376. return -ENOMEM;
  377. uncorepmu->dev = dev;
  378. uncorepmu->cpu = cpumask_local_spread(0, dev_to_node(dev));
  379. platform_set_drvdata(pdev, uncorepmu);
  380. switch (device_type) {
  381. case FUJITSU_UNCORE_PMU_MAC:
  382. uncorepmu->num_counters = MAC_NUM_COUNTERS;
  383. attr_groups = fujitsu_uncore_mac_pmu_attr_grps;
  384. name = devm_kasprintf(dev, GFP_KERNEL, "mac_iod%llu_mac%llu_ch%llu",
  385. (uid >> 8) & 0xF, (uid >> 4) & 0xF, uid & 0xF);
  386. break;
  387. case FUJITSU_UNCORE_PMU_PCI:
  388. uncorepmu->num_counters = PCI_NUM_COUNTERS;
  389. attr_groups = fujitsu_uncore_pci_pmu_attr_grps;
  390. name = devm_kasprintf(dev, GFP_KERNEL, "pci_iod%llu_pci%llu",
  391. (uid >> 4) & 0xF, uid & 0xF);
  392. break;
  393. default:
  394. return dev_err_probe(dev, -EINVAL, "illegal device type: %lu\n", device_type);
  395. }
  396. if (!name)
  397. return -ENOMEM;
  398. uncorepmu->pmu = (struct pmu) {
  399. .parent = dev,
  400. .task_ctx_nr = perf_invalid_context,
  401. .attr_groups = attr_groups,
  402. .pmu_enable = fujitsu_uncore_pmu_enable,
  403. .pmu_disable = fujitsu_uncore_pmu_disable,
  404. .event_init = fujitsu_uncore_event_init,
  405. .add = fujitsu_uncore_event_add,
  406. .del = fujitsu_uncore_event_del,
  407. .start = fujitsu_uncore_event_start,
  408. .stop = fujitsu_uncore_event_stop,
  409. .read = fujitsu_uncore_event_read,
  410. .capabilities = PERF_PMU_CAP_NO_EXCLUDE | PERF_PMU_CAP_NO_INTERRUPT,
  411. };
  412. alloc_size = sizeof(uncorepmu->events[0]) * uncorepmu->num_counters;
  413. uncorepmu->events = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
  414. if (!uncorepmu->events)
  415. return -ENOMEM;
  416. alloc_size = sizeof(uncorepmu->used_mask[0]) * BITS_TO_LONGS(uncorepmu->num_counters);
  417. uncorepmu->used_mask = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
  418. if (!uncorepmu->used_mask)
  419. return -ENOMEM;
  420. uncorepmu->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &memrc);
  421. if (IS_ERR(uncorepmu->regs))
  422. return PTR_ERR(uncorepmu->regs);
  423. fujitsu_uncore_init(uncorepmu);
  424. irq = platform_get_irq(pdev, 0);
  425. if (irq < 0)
  426. return irq;
  427. ret = devm_request_irq(dev, irq, fujitsu_uncore_handle_irq,
  428. IRQF_NOBALANCING | IRQF_NO_THREAD,
  429. name, uncorepmu);
  430. if (ret)
  431. return dev_err_probe(dev, ret, "Failed to request IRQ:%d\n", irq);
  432. ret = irq_set_affinity(irq, cpumask_of(uncorepmu->cpu));
  433. if (ret)
  434. return dev_err_probe(dev, ret, "Failed to set irq affinity:%d\n", irq);
  435. uncorepmu->irq = irq;
  436. /* Add this instance to the list used by the offline callback */
  437. ret = cpuhp_state_add_instance(uncore_pmu_cpuhp_state, &uncorepmu->node);
  438. if (ret)
  439. return dev_err_probe(dev, ret, "Error registering hotplug");
  440. ret = perf_pmu_register(&uncorepmu->pmu, name, -1);
  441. if (ret < 0) {
  442. cpuhp_state_remove_instance_nocalls(uncore_pmu_cpuhp_state, &uncorepmu->node);
  443. return dev_err_probe(dev, ret, "Failed to register %s PMU\n", name);
  444. }
  445. dev_dbg(dev, "Registered %s, type: %d\n", name, uncorepmu->pmu.type);
  446. return 0;
  447. }
  448. static void fujitsu_uncore_pmu_remove(struct platform_device *pdev)
  449. {
  450. struct uncore_pmu *uncorepmu = platform_get_drvdata(pdev);
  451. writeq_relaxed(0, uncorepmu->regs + PM_CR);
  452. perf_pmu_unregister(&uncorepmu->pmu);
  453. cpuhp_state_remove_instance_nocalls(uncore_pmu_cpuhp_state, &uncorepmu->node);
  454. }
  455. static const struct acpi_device_id fujitsu_uncore_pmu_acpi_match[] = {
  456. { "FUJI200C", FUJITSU_UNCORE_PMU_MAC },
  457. { "FUJI200D", FUJITSU_UNCORE_PMU_PCI },
  458. { }
  459. };
  460. MODULE_DEVICE_TABLE(acpi, fujitsu_uncore_pmu_acpi_match);
  461. static struct platform_driver fujitsu_uncore_pmu_driver = {
  462. .driver = {
  463. .name = "fujitsu-uncore-pmu",
  464. .acpi_match_table = fujitsu_uncore_pmu_acpi_match,
  465. .suppress_bind_attrs = true,
  466. },
  467. .probe = fujitsu_uncore_pmu_probe,
  468. .remove = fujitsu_uncore_pmu_remove,
  469. };
  470. static int __init fujitsu_uncore_pmu_init(void)
  471. {
  472. int ret;
  473. /* Install a hook to update the reader CPU in case it goes offline */
  474. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  475. "perf/fujitsu/uncore:online",
  476. fujitsu_uncore_pmu_online_cpu,
  477. fujitsu_uncore_pmu_offline_cpu);
  478. if (ret < 0)
  479. return ret;
  480. uncore_pmu_cpuhp_state = ret;
  481. ret = platform_driver_register(&fujitsu_uncore_pmu_driver);
  482. if (ret)
  483. cpuhp_remove_multi_state(uncore_pmu_cpuhp_state);
  484. return ret;
  485. }
  486. static void __exit fujitsu_uncore_pmu_exit(void)
  487. {
  488. platform_driver_unregister(&fujitsu_uncore_pmu_driver);
  489. cpuhp_remove_multi_state(uncore_pmu_cpuhp_state);
  490. }
  491. module_init(fujitsu_uncore_pmu_init);
  492. module_exit(fujitsu_uncore_pmu_exit);
  493. MODULE_AUTHOR("Koichi Okuno <fj2767dz@fujitsu.com>");
  494. MODULE_DESCRIPTION("Fujitsu Uncore PMU driver");
  495. MODULE_LICENSE("GPL");