summaryrefslogtreecommitdiffstats
path: root/drivers/perf/starfive_starlink_pmu.c
blob: 5e5a672b42294e9eb4ee906f1d6878475494a885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// SPDX-License-Identifier: GPL-2.0-only
/*
 * StarFive's StarLink PMU driver
 *
 * Copyright (C) 2023 StarFive Technology Co., Ltd.
 *
 * Author: Ji Sheng Teoh <jisheng.teoh@starfivetech.com>
 *
 */

#define STARLINK_PMU_PDEV_NAME	"starfive_starlink_pmu"
#define pr_fmt(fmt)	STARLINK_PMU_PDEV_NAME ": " fmt

#include <linux/bitmap.h>
#include <linux/cpu_pm.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/perf_event.h>
#include <linux/platform_device.h>
#include <linux/sysfs.h>

#define STARLINK_PMU_MAX_COUNTERS			64
#define STARLINK_PMU_NUM_COUNTERS			16
#define STARLINK_PMU_IDX_CYCLE_COUNTER			63

#define STARLINK_PMU_EVENT_SELECT			0x060
#define STARLINK_PMU_EVENT_COUNTER			0x160
#define STARLINK_PMU_COUNTER_MASK			GENMASK_ULL(63, 0)
#define STARLINK_PMU_CYCLE_COUNTER			0x058

#define STARLINK_PMU_CONTROL				0x040
#define STARLINK_PMU_GLOBAL_ENABLE			BIT_ULL(0)

#define STARLINK_PMU_INTERRUPT_ENABLE			0x050
#define STARLINK_PMU_COUNTER_OVERFLOW_STATUS		0x048
#define STARLINK_PMU_CYCLE_OVERFLOW_MASK		BIT_ULL(63)

#define STARLINK_CYCLES				0x058
#define CACHE_READ_REQUEST			0x04000701
#define CACHE_WRITE_REQUEST			0x03000001
#define CACHE_RELEASE_REQUEST			0x0003e001
#define CACHE_READ_HIT				0x00901202
#define CACHE_READ_MISS				0x04008002
#define CACHE_WRITE_HIT				0x006c0002
#define CACHE_WRITE_MISS			0x03000002
#define CACHE_WRITEBACK				0x00000403

#define to_starlink_pmu(p) (container_of(p, struct starlink_pmu, pmu))

#define STARLINK_FORMAT_ATTR(_name, _config)				      \
	(&((struct dev_ext_attribute[]) {				      \
		{ .attr = __ATTR(_name, 0444, starlink_pmu_sysfs_format_show, NULL), \
		  .var = (void *)_config, }				      \
	})[0].attr.attr)

#define STARLINK_EVENT_ATTR(_name, _id)					     \
	PMU_EVENT_ATTR_ID(_name, starlink_pmu_sysfs_event_show, _id)

static int starlink_pmu_cpuhp_state;

struct starlink_hw_events {
	struct perf_event	*events[STARLINK_PMU_MAX_COUNTERS];
	DECLARE_BITMAP(used_mask, STARLINK_PMU_MAX_COUNTERS);
};

struct starlink_pmu {
	struct pmu					pmu;
	struct starlink_hw_events			__percpu *hw_events;
	struct hlist_node				node;
	struct notifier_block				starlink_pmu_pm_nb;
	void __iomem					*pmu_base;
	cpumask_t					cpumask;
	int						irq;
};

static ssize_t
starlink_pmu_sysfs_format_show(struct device *dev,
			       struct device_attribute *attr,
			       char *buf)
{
	struct dev_ext_attribute *eattr = container_of(attr,
						       struct dev_ext_attribute, attr);

	return sysfs_emit(buf, "%s\n", (char *)eattr->var);
}

static struct attribute *starlink_pmu_format_attrs[] = {
	STARLINK_FORMAT_ATTR(event, "config:0-31"),
	NULL
};

static const struct attribute_group starlink_pmu_format_attr_group = {
	.name = "format",
	.attrs = starlink_pmu_format_attrs,
};

static ssize_t
starlink_pmu_sysfs_event_show(struct device *dev,
			      struct device_attribute *attr,
			      char *buf)
{
	struct perf_pmu_events_attr *eattr = container_of(attr,
							  struct perf_pmu_events_attr, attr);

	return sysfs_emit(buf, "event=0x%02llx\n", eattr->id);
}

static struct attribute *starlink_pmu_event_attrs[] = {
	STARLINK_EVENT_ATTR(cycles, STARLINK_CYCLES),
	STARLINK_EVENT_ATTR(read_request, CACHE_READ_REQUEST),
	STARLINK_EVENT_ATTR(write_request, CACHE_WRITE_REQUEST),
	STARLINK_EVENT_ATTR(release_request, CACHE_RELEASE_REQUEST),
	STARLINK_EVENT_ATTR(read_hit, CACHE_READ_HIT),
	STARLINK_EVENT_ATTR(read_miss, CACHE_READ_MISS),
	STARLINK_EVENT_ATTR(write_hit, CACHE_WRITE_HIT),
	STARLINK_EVENT_ATTR(write_miss, CACHE_WRITE_MISS),
	STARLINK_EVENT_ATTR(writeback, CACHE_WRITEBACK),
	NULL
};

static const struct attribute_group starlink_pmu_events_attr_group = {
	.name = "events",
	.attrs = starlink_pmu_event_attrs,
};

static ssize_t
cpumask_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct starlink_pmu *starlink_pmu = to_starlink_pmu(dev_get_drvdata(dev));

	return cpumap_print_to_pagebuf(true, buf, &starlink_pmu->cpumask);
}

static DEVICE_ATTR_RO(cpumask);

static struct attribute *starlink_pmu_cpumask_attrs[] = {
	&dev_attr_cpumask.attr,
	NULL
};

static const struct attribute_group starlink_pmu_cpumask_attr_group = {
	.attrs = starlink_pmu_cpumask_attrs,
};

static const struct attribute_group *starlink_pmu_attr_groups[] = {
	&starlink_pmu_format_attr_group,
	&starlink_pmu_events_attr_group,
	&starlink_pmu_cpumask_attr_group,
	NULL
};

static void starlink_pmu_set_event_period(struct perf_event *event)
{
	struct starlink_pmu *starlink_pmu = to_starlink_pmu(event->pmu);
	struct hw_perf_event *hwc = &event->hw;
	int idx = event->hw.idx;

	/*
	 * Program counter to half of it's max count to handle
	 * cases of extreme interrupt latency.
	 */
	u64 val = STARLINK_PMU_COUNTER_MASK >> 1;

	local64_set(&hwc->prev_count, val);
	if (hwc->config == STARLINK_CYCLES)
		writeq(val, starlink_pmu->pmu_base + STARLINK_PMU_CYCLE_COUNTER);
	else
		writeq(val, starlink_pmu->pmu_base + STARLINK_PMU_EVENT_COUNTER +
		       idx * sizeof(u64));
}

static void starlink_pmu_counter_start(struct perf_event *event,
				       struct starlink_pmu *starlink_pmu)
{
	struct hw_perf_event *hwc = &event->hw;
	int idx = event->hw.idx;
	u64 val;

	/*
	 * Enable counter overflow interrupt[63:0],
	 * which is mapped as follow:
	 *
	 * event counter 0	- Bit [0]
	 * event counter 1	- Bit [1]
	 * ...
	 * cycle counter	- Bit [63]
	 */
	val = readq(starlink_pmu->pmu_base + STARLINK_PMU_INTERRUPT_ENABLE);

	if (hwc->config == STARLINK_CYCLES) {
		/*
		 * Cycle count has its dedicated register, and it starts
		 * counting as soon as STARLINK_PMU_GLOBAL_ENABLE is set.
		 */
		val |= STARLINK_PMU_CYCLE_OVERFLOW_MASK;
	} else {
		writeq(event->hw.config, starlink_pmu->pmu_base +
		       STARLINK_PMU_EVENT_SELECT + idx * sizeof(u64));

		val |= BIT_ULL(idx);
	}

	writeq(val, starlink_pmu->pmu_base + STARLINK_PMU_INTERRUPT_ENABLE);

	writeq(STARLINK_PMU_GLOBAL_ENABLE, starlink_pmu->pmu_base +
	       STARLINK_PMU_CONTROL);
}

static void starlink_pmu_counter_stop(struct perf_event *event,
				      struct starlink_pmu *starlink_pmu)
{
	struct hw_perf_event *hwc = &event->hw;
	int idx = event->hw.idx;
	u64 val;

	val = readq(starlink_pmu->pmu_base + STARLINK_PMU_CONTROL);
	val &= ~STARLINK_PMU_GLOBAL_ENABLE;
	writeq(val, starlink_pmu->pmu_base + STARLINK_PMU_CONTROL);

	val = readq(starlink_pmu->pmu_base + STARLINK_PMU_INTERRUPT_ENABLE);
	if (hwc->config == STARLINK_CYCLES)
		val &= ~STARLINK_PMU_CYCLE_OVERFLOW_MASK;
	else
		val &= ~BIT_ULL(idx);

	writeq(val, starlink_pmu->pmu_base + STARLINK_PMU_INTERRUPT_ENABLE);
}

static void starlink_pmu_update(struct perf_event *event)
{
	struct starlink_pmu *starlink_pmu = to_starlink_pmu(event->pmu);
	struct hw_perf_event *hwc = &event->hw;
	int idx = hwc->idx;
	u64 prev_raw_count, new_raw_count;
	u64 oldval;
	u64 delta;

	do {
		prev_raw_count = local64_read(&hwc->prev_count);
		if (hwc->config == STARLINK_CYCLES)
			new_raw_count = readq(starlink_pmu->pmu_base +
					      STARLINK_PMU_CYCLE_COUNTER);
		else
			new_raw_count = readq(starlink_pmu->pmu_base +
					      STARLINK_PMU_EVENT_COUNTER +
					      idx * sizeof(u64));
		oldval = local64_cmpxchg(&hwc->prev_count, prev_raw_count,
					 new_raw_count);
	} while (oldval != prev_raw_count);

	delta = (new_raw_count - prev_raw_count) & STARLINK_PMU_COUNTER_MASK;
	local64_add(delta, &event->count);
}

static void starlink_pmu_start(struct perf_event *event, int flags)
{
	struct starlink_pmu *starlink_pmu = to_starlink_pmu(event->pmu);
	struct hw_perf_event *hwc = &event->hw;

	if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
		return;

	if (flags & PERF_EF_RELOAD)
		WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));

	hwc->state = 0;

	starlink_pmu_set_event_period(event);
	starlink_pmu_counter_start(event, starlink_pmu);

	perf_event_update_userpage(event);
}

static void starlink_pmu_stop(struct perf_event *event, int flags)
{
	struct starlink_pmu *starlink_pmu = to_starlink_pmu(event->pmu);
	struct hw_perf_event *hwc = &event->hw;

	if (hwc->state & PERF_HES_STOPPED)
		return;

	starlink_pmu_counter_stop(event, starlink_pmu);
	starlink_pmu_update(event);
	hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
}

static int starlink_pmu_add(struct perf_event *event, int flags)
{
	struct starlink_pmu *starlink_pmu = to_starlink_pmu(event->pmu);
	struct starlink_hw_events *hw_events =
					this_cpu_ptr(starlink_pmu->hw_events);
	struct hw_perf_event *hwc = &event->hw;
	unsigned long *used_mask = hw_events->used_mask;
	u32 n_events = STARLINK_PMU_NUM_COUNTERS;
	int idx;

	/*
	 * Cycle counter has dedicated register to hold counter value.
	 * Event other than cycle count has to be enabled through
	 * event select register, and assigned with independent counter
	 * as they appear.
	 */

	if (hwc->config == STARLINK_CYCLES) {
		idx = STARLINK_PMU_IDX_CYCLE_COUNTER;
	} else {
		idx = find_first_zero_bit(used_mask, n_events);
		/* All counter are in use */
		if (idx < 0)
			return idx;

		set_bit(idx, used_mask);
	}

	hwc->idx = idx;
	hw_events->events[idx] = event;
	hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;

	if (flags & PERF_EF_START)
		starlink_pmu_start(event, PERF_EF_RELOAD);

	perf_event_update_userpage(event);

	return 0;
}

static void starlink_pmu_del(struct perf_event *event, int flags)
{
	struct starlink_pmu *starlink_pmu = to_starlink_pmu(event->pmu);
	struct starlink_hw_events *hw_events =
					this_cpu_ptr(starlink_pmu->hw_events);
	struct hw_perf_event *hwc = &event->hw;

	starlink_pmu_stop(event, PERF_EF_UPDATE);
	hw_events->events[hwc->idx] = NULL;
	clear_bit(hwc->idx, hw_events->used_mask);

	perf_event_update_userpage(event);
}

static bool starlink_pmu_validate_event_group(struct perf_event *event)
{
	struct perf_event *leader = event->group_leader;
	struct perf_event *sibling;
	int counter = 1;

	/*
	 * Ensure hardware events in the group are on the same PMU,
	 * software events are acceptable.
	 */
	if (event->group_leader->pmu != event->pmu &&
	    !is_software_event(event->group_leader))
		return false;

	for_each_sibling_event(sibling, leader) {
		if (sibling->pmu != event->pmu && !is_software_event(sibling))
			return false;

		counter++;
	}

	return counter <= STARLINK_PMU_NUM_COUNTERS;
}

static int starlink_pmu_event_init(struct perf_event *event)
{
	struct starlink_pmu *starlink_pmu = to_starlink_pmu(event->pmu);
	struct hw_perf_event *hwc = &event->hw;

	/*
	 * Sampling is not supported, as counters are shared
	 * by all CPU.
	 */
	if (hwc->sample_period)
		return -EOPNOTSUPP;

	/*
	 * Per-task and attach to a task are not supported,
	 * as uncore events are not specific to any CPU.
	 */
	if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK)
		return -EOPNOTSUPP;

	if (!starlink_pmu_validate_event_group(event))
		return -EINVAL;

	hwc->idx = -1;
	hwc->config = event->attr.config;
	event->cpu = cpumask_first(&starlink_pmu->cpumask);

	return 0;
}

static irqreturn_t starlink_pmu_handle_irq(int irq_num, void *data)
{
	struct starlink_pmu *starlink_pmu = data;
	struct starlink_hw_events *hw_events =
			this_cpu_ptr(starlink_pmu->hw_events);
	bool handled = false;
	int idx;
	u64 overflow_status;

	for (idx = 0; idx < STARLINK_PMU_MAX_COUNTERS; idx++) {
		struct perf_event *event = hw_events->events[idx];

		if (!event)
			continue;

		overflow_status = readq(starlink_pmu->pmu_base +
					STARLINK_PMU_COUNTER_OVERFLOW_STATUS);
		if (!(overflow_status & BIT_ULL(idx)))
			continue;

		writeq(BIT_ULL(idx), starlink_pmu->pmu_base +
		       STARLINK_PMU_COUNTER_OVERFLOW_STATUS);

		starlink_pmu_update(event);
		starlink_pmu_set_event_period(event);
		handled = true;
	}
	return IRQ_RETVAL(handled);
}

static int starlink_setup_irqs(struct starlink_pmu *starlink_pmu,
			       struct platform_device *pdev)
{
	int ret, irq;

	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
		return -EINVAL;

	ret = devm_request_irq(&pdev->dev, irq, starlink_pmu_handle_irq,
			       0, STARLINK_PMU_PDEV_NAME, starlink_pmu);
	if (ret)
		return dev_err_probe(&pdev->dev, ret, "Failed to request IRQ\n");

	starlink_pmu->irq = irq;

	return 0;
}

static int starlink_pmu_pm_notify(struct notifier_block *b,
				  unsigned long cmd, void *v)
{
	struct starlink_pmu *starlink_pmu = container_of(b, struct starlink_pmu,
							 starlink_pmu_pm_nb);
	struct starlink_hw_events *hw_events =
					this_cpu_ptr(starlink_pmu->hw_events);
	int enabled = bitmap_weight(hw_events->used_mask,
				    STARLINK_PMU_MAX_COUNTERS);
	struct perf_event *event;
	int idx;

	if (!enabled)
		return NOTIFY_OK;

	for (idx = 0; idx < STARLINK_PMU_MAX_COUNTERS; idx++) {
		event = hw_events->events[idx];
		if (!event)
			continue;

		switch (cmd) {
		case CPU_PM_ENTER:
			/* Stop and update the counter */
			starlink_pmu_stop(event, PERF_EF_UPDATE);
			break;
		case CPU_PM_EXIT:
		case CPU_PM_ENTER_FAILED:
			/* Restore and enable the counter */
			starlink_pmu_start(event, PERF_EF_RELOAD);
			break;
		default:
			break;
		}
	}

	return NOTIFY_OK;
}

static int starlink_pmu_pm_register(struct starlink_pmu *starlink_pmu)
{
	if (!IS_ENABLED(CONFIG_CPU_PM))
		return 0;

	starlink_pmu->starlink_pmu_pm_nb.notifier_call = starlink_pmu_pm_notify;
	return cpu_pm_register_notifier(&starlink_pmu->starlink_pmu_pm_nb);
}

static void starlink_pmu_pm_unregister(struct starlink_pmu *starlink_pmu)
{
	if (!IS_ENABLED(CONFIG_CPU_PM))
		return;

	cpu_pm_unregister_notifier(&starlink_pmu->starlink_pmu_pm_nb);
}

static void starlink_pmu_destroy(struct starlink_pmu *starlink_pmu)
{
	starlink_pmu_pm_unregister(starlink_pmu);
	cpuhp_state_remove_instance(starlink_pmu_cpuhp_state,
				    &starlink_pmu->node);
}

static int starlink_pmu_probe(struct platform_device *pdev)
{
	struct starlink_pmu *starlink_pmu;
	struct starlink_hw_events *hw_events;
	struct resource *res;
	int cpuid, i, ret;

	starlink_pmu = devm_kzalloc(&pdev->dev, sizeof(*starlink_pmu), GFP_KERNEL);
	if (!starlink_pmu)
		return -ENOMEM;

	starlink_pmu->pmu_base =
			devm_platform_get_and_ioremap_resource(pdev, 0, &res);
	if (IS_ERR(starlink_pmu->pmu_base))
		return PTR_ERR(starlink_pmu->pmu_base);

	starlink_pmu->hw_events = alloc_percpu_gfp(struct starlink_hw_events,
						   GFP_KERNEL);
	if (!starlink_pmu->hw_events) {
		dev_err(&pdev->dev, "Failed to allocate per-cpu PMU data\n");
		return -ENOMEM;
	}

	for_each_possible_cpu(cpuid) {
		hw_events = per_cpu_ptr(starlink_pmu->hw_events, cpuid);
		for (i = 0; i < STARLINK_PMU_MAX_COUNTERS; i++)
			hw_events->events[i] = NULL;
	}

	ret = starlink_setup_irqs(starlink_pmu, pdev);
	if (ret)
		return ret;

	ret = cpuhp_state_add_instance(starlink_pmu_cpuhp_state,
				       &starlink_pmu->node);
	if (ret) {
		dev_err(&pdev->dev, "Failed to register hotplug\n");
		return ret;
	}

	ret = starlink_pmu_pm_register(starlink_pmu);
	if (ret) {
		cpuhp_state_remove_instance(starlink_pmu_cpuhp_state,
					    &starlink_pmu->node);
		return ret;
	}

	starlink_pmu->pmu = (struct pmu) {
		.task_ctx_nr	= perf_invalid_context,
		.event_init	= starlink_pmu_event_init,
		.add		= starlink_pmu_add,
		.del		= starlink_pmu_del,
		.start		= starlink_pmu_start,
		.stop		= starlink_pmu_stop,
		.read		= starlink_pmu_update,
		.attr_groups	= starlink_pmu_attr_groups,
	};

	ret = perf_pmu_register(&starlink_pmu->pmu, STARLINK_PMU_PDEV_NAME, -1);
	if (ret)
		starlink_pmu_destroy(starlink_pmu);

	return ret;
}

static const struct of_device_id starlink_pmu_of_match[] = {
	{ .compatible = "starfive,jh8100-starlink-pmu" },
	{}
};
MODULE_DEVICE_TABLE(of, starlink_pmu_of_match);

static struct platform_driver starlink_pmu_driver = {
	.driver = {
		.name	= STARLINK_PMU_PDEV_NAME,
		.of_match_table = starlink_pmu_of_match,
		.suppress_bind_attrs = true,
	},
	.probe = starlink_pmu_probe,
};

static int
starlink_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
{
	struct starlink_pmu *starlink_pmu = hlist_entry_safe(node,
							     struct starlink_pmu,
							     node);

	if (cpumask_empty(&starlink_pmu->cpumask))
		cpumask_set_cpu(cpu, &starlink_pmu->cpumask);

	WARN_ON(irq_set_affinity(starlink_pmu->irq, cpumask_of(cpu)));

	return 0;
}

static int
starlink_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
{
	struct starlink_pmu *starlink_pmu = hlist_entry_safe(node,
							     struct starlink_pmu,
							     node);
	unsigned int target;

	if (!cpumask_test_and_clear_cpu(cpu, &starlink_pmu->cpumask))
		return 0;

	target = cpumask_any_but(cpu_online_mask, cpu);
	if (target >= nr_cpu_ids)
		return 0;

	perf_pmu_migrate_context(&starlink_pmu->pmu, cpu, target);

	cpumask_set_cpu(target, &starlink_pmu->cpumask);
	WARN_ON(irq_set_affinity(starlink_pmu->irq, cpumask_of(target)));

	return 0;
}

static int __init starlink_pmu_init(void)
{
	int ret;

	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
				      "soc/starfive/starlink_pmu:online",
				      starlink_pmu_online_cpu,
				      starlink_pmu_offline_cpu);
	if (ret < 0)
		return ret;

	starlink_pmu_cpuhp_state = ret;

	return platform_driver_register(&starlink_pmu_driver);
}

device_initcall(starlink_pmu_init);