xref: /linux/drivers/perf/arm_cspmu/arm_cspmu.c (revision ae814200e8393fa504dd246e98fcba8f5493de28)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ARM CoreSight Architecture PMU driver.
4  *
5  * This driver adds support for uncore PMU based on ARM CoreSight Performance
6  * Monitoring Unit Architecture. The PMU is accessible via MMIO registers and
7  * like other uncore PMUs, it does not support process specific events and
8  * cannot be used in sampling mode.
9  *
10  * This code is based on other uncore PMUs like ARM DSU PMU. It provides a
11  * generic implementation to operate the PMU according to CoreSight PMU
12  * architecture and ACPI ARM PMU table (APMT) documents below:
13  *   - ARM CoreSight PMU architecture document number: ARM IHI 0091 A.a-00bet0.
14  *   - APMT document number: ARM DEN0117.
15  *
16  * The user should refer to the vendor technical documentation to get details
17  * about the supported events.
18  *
19  * Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
20  *
21  */
22 
23 #include <linux/acpi.h>
24 #include <linux/cacheinfo.h>
25 #include <linux/ctype.h>
26 #include <linux/interrupt.h>
27 #include <linux/io-64-nonatomic-lo-hi.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/of.h>
31 #include <linux/perf_event.h>
32 #include <linux/platform_device.h>
33 
34 #include "arm_cspmu.h"
35 
36 #define PMUNAME "arm_cspmu"
37 #define DRVNAME "arm-cs-arch-pmu"
38 
39 #define ARM_CSPMU_CPUMASK_ATTR(_name, _config)			\
40 	ARM_CSPMU_EXT_ATTR(_name, arm_cspmu_cpumask_show,	\
41 				(unsigned long)_config)
42 
43 /* Each SET/CLR register supports up to 32 counters. */
44 #define ARM_CSPMU_SET_CLR_COUNTER_SHIFT		5
45 #define ARM_CSPMU_SET_CLR_COUNTER_NUM		\
46 	(1 << ARM_CSPMU_SET_CLR_COUNTER_SHIFT)
47 
48 /* Convert counter idx into SET/CLR register number. */
49 #define COUNTER_TO_SET_CLR_ID(idx)			\
50 	(idx >> ARM_CSPMU_SET_CLR_COUNTER_SHIFT)
51 
52 /* Convert counter idx into SET/CLR register bit. */
53 #define COUNTER_TO_SET_CLR_BIT(idx)			\
54 	(idx & (ARM_CSPMU_SET_CLR_COUNTER_NUM - 1))
55 
56 #define ARM_CSPMU_ACTIVE_CPU_MASK		0x0
57 #define ARM_CSPMU_ASSOCIATED_CPU_MASK		0x1
58 
59 /*
60  * Maximum poll count for reading counter value using high-low-high sequence.
61  */
62 #define HILOHI_MAX_POLL	1000
63 
64 static unsigned long arm_cspmu_cpuhp_state;
65 
66 static DEFINE_MUTEX(arm_cspmu_lock);
67 
68 static void arm_cspmu_set_ev_filter(struct arm_cspmu *cspmu,
69 				    const struct perf_event *event);
70 static void arm_cspmu_set_cc_filter(struct arm_cspmu *cspmu,
71 				    const struct perf_event *event);
72 
arm_cspmu_apmt_node(struct device * dev)73 static struct acpi_apmt_node *arm_cspmu_apmt_node(struct device *dev)
74 {
75 	struct acpi_apmt_node **ptr = dev_get_platdata(dev);
76 
77 	return ptr ? *ptr : NULL;
78 }
79 
80 /*
81  * With FEAT_CSPMU_EXT32, all of the MMIO registers are 32-bit except the
82  * counter registers, which are either 32-bit or 64-bit depending on the value
83  * of PMCFGR.SIZE. It is implementation-defined whether single-copy-atomic
84  * 64-bit accesses are supported, so we rely on a firmware flag to identify
85  * that, and otherwise treat a 64-bit counter as a non-atomic pair of 32-bit
86  * registers. With FEAT_CSPMU_EXT64, everything is 64-bit, but we may still
87  * have to deal with atomicity being broken.
88  */
89 
90 /*
91  * Read 64-bit register as a pair of 32-bit registers using hi-lo-hi sequence.
92  */
read_reg64_hilohi(const void __iomem * addr,u32 max_poll_count)93 static u64 read_reg64_hilohi(const void __iomem *addr, u32 max_poll_count)
94 {
95 	u32 val_lo, val_hi;
96 	u64 val;
97 
98 	/* Use high-low-high sequence to avoid tearing */
99 	do {
100 		if (max_poll_count-- == 0) {
101 			pr_err("ARM CSPMU: timeout hi-low-high sequence\n");
102 			return 0;
103 		}
104 
105 		val_hi = readl(addr + 4);
106 		val_lo = readl(addr);
107 	} while (val_hi != readl(addr + 4));
108 
109 	val = (((u64)val_hi << 32) | val_lo);
110 
111 	return val;
112 }
113 
114 /* Check if cycle counter is supported. */
supports_cycle_counter(const struct arm_cspmu * cspmu)115 static inline bool supports_cycle_counter(const struct arm_cspmu *cspmu)
116 {
117 	return (cspmu->pmcfgr & PMCFGR_CC);
118 }
119 
120 /* Get counter size, which is (PMCFGR_SIZE + 1). */
counter_size(const struct arm_cspmu * cspmu)121 static inline u32 counter_size(const struct arm_cspmu *cspmu)
122 {
123 	return FIELD_GET(PMCFGR_SIZE, cspmu->pmcfgr) + 1;
124 }
125 
126 /* Get counter mask. */
counter_mask(const struct arm_cspmu * cspmu)127 static inline u64 counter_mask(const struct arm_cspmu *cspmu)
128 {
129 	return GENMASK_ULL(counter_size(cspmu) - 1, 0);
130 }
131 
132 /* Check if counter is implemented as 64-bit register. */
use_64b_counter_reg(const struct arm_cspmu * cspmu)133 static inline bool use_64b_counter_reg(const struct arm_cspmu *cspmu)
134 {
135 	return (counter_size(cspmu) > 32);
136 }
137 
arm_cspmu_sysfs_event_show(struct device * dev,struct device_attribute * attr,char * buf)138 ssize_t arm_cspmu_sysfs_event_show(struct device *dev,
139 				struct device_attribute *attr, char *buf)
140 {
141 	struct perf_pmu_events_attr *pmu_attr;
142 
143 	pmu_attr = container_of(attr, typeof(*pmu_attr), attr);
144 	return sysfs_emit(buf, "event=0x%llx\n", pmu_attr->id);
145 }
146 EXPORT_SYMBOL_GPL(arm_cspmu_sysfs_event_show);
147 
148 /* Default event list. */
149 static struct attribute *arm_cspmu_event_attrs[] = {
150 	ARM_CSPMU_EVENT_ATTR(cycles, ARM_CSPMU_EVT_CYCLES_DEFAULT),
151 	NULL,
152 };
153 
154 static struct attribute **
arm_cspmu_get_event_attrs(const struct arm_cspmu * cspmu)155 arm_cspmu_get_event_attrs(const struct arm_cspmu *cspmu)
156 {
157 	struct attribute **attrs;
158 
159 	attrs = devm_kmemdup(cspmu->dev, arm_cspmu_event_attrs,
160 		sizeof(arm_cspmu_event_attrs), GFP_KERNEL);
161 
162 	return attrs;
163 }
164 
165 static umode_t
arm_cspmu_event_attr_is_visible(struct kobject * kobj,struct attribute * attr,int unused)166 arm_cspmu_event_attr_is_visible(struct kobject *kobj,
167 				struct attribute *attr, int unused)
168 {
169 	struct device *dev = kobj_to_dev(kobj);
170 	struct arm_cspmu *cspmu = to_arm_cspmu(dev_get_drvdata(dev));
171 	struct perf_pmu_events_attr *eattr;
172 
173 	eattr = container_of(attr, typeof(*eattr), attr.attr);
174 
175 	/* Hide cycle event if not supported */
176 	if ((cspmu->has_ext64 || !supports_cycle_counter(cspmu)) &&
177 	    eattr->id == ARM_CSPMU_EVT_CYCLES_DEFAULT)
178 		return 0;
179 
180 	return attr->mode;
181 }
182 
arm_cspmu_default_format_show(struct device * dev,struct device_attribute * attr,char * buf)183 ssize_t arm_cspmu_default_format_show(struct device *dev,
184 				      struct device_attribute *attr, char *buf)
185 {
186 	struct perf_pmu_events_attr *fmt = container_of(attr, typeof(*fmt), attr);
187 	struct arm_cspmu *cspmu = to_arm_cspmu(dev_get_drvdata(dev));
188 	u64 field = cspmu->has_ext64 ? U64_MAX : U32_MAX;
189 	DECLARE_BITMAP(bits, 64) = { BITMAP_FROM_U64(field) };
190 
191 	if (!fmt->id) {
192 		set_bit(32, bits); /* For 32-bit "cycles" event */
193 		return sysfs_emit(buf, "config:%*pbl\n", 64, bits);
194 	}
195 
196 	return sysfs_emit(buf, "config%lld:%*pbl\n", fmt->id, 64, bits);
197 }
198 EXPORT_SYMBOL_GPL(arm_cspmu_default_format_show);
199 
200 static struct attribute *arm_cspmu_format_attrs[] = {
201 	ARM_CSPMU_FORMAT_EVENT_ATTR,
202 	ARM_CSPMU_FORMAT_FILTER_ATTR,
203 	ARM_CSPMU_FORMAT_FILTER2_ATTR,
204 	NULL,
205 };
206 
207 static struct attribute **
arm_cspmu_get_format_attrs(const struct arm_cspmu * cspmu)208 arm_cspmu_get_format_attrs(const struct arm_cspmu *cspmu)
209 {
210 	struct attribute **attrs;
211 
212 	attrs = devm_kmemdup(cspmu->dev, arm_cspmu_format_attrs,
213 		sizeof(arm_cspmu_format_attrs), GFP_KERNEL);
214 
215 	return attrs;
216 }
217 
arm_cspmu_event_type(const struct perf_event * event)218 static u64 arm_cspmu_event_type(const struct perf_event *event)
219 {
220 	return event->attr.config;
221 }
222 
arm_cspmu_is_cycle_counter_event(const struct perf_event * event)223 static bool arm_cspmu_is_cycle_counter_event(const struct perf_event *event)
224 {
225 	return (event->attr.config == ARM_CSPMU_EVT_CYCLES_DEFAULT);
226 }
227 
arm_cspmu_filter(const struct perf_event * event)228 static u64 arm_cspmu_filter(const struct perf_event *event)
229 {
230 	return event->attr.config1;
231 }
232 
arm_cspmu_filter2(const struct perf_event * event)233 static u64 arm_cspmu_filter2(const struct perf_event *event)
234 {
235 	return event->attr.config2;
236 }
237 
arm_cspmu_identifier_show(struct device * dev,struct device_attribute * attr,char * page)238 static ssize_t arm_cspmu_identifier_show(struct device *dev,
239 					 struct device_attribute *attr,
240 					 char *page)
241 {
242 	struct arm_cspmu *cspmu = to_arm_cspmu(dev_get_drvdata(dev));
243 
244 	return sysfs_emit(page, "%s\n", cspmu->identifier);
245 }
246 
247 static struct device_attribute arm_cspmu_identifier_attr =
248 	__ATTR(identifier, 0444, arm_cspmu_identifier_show, NULL);
249 
250 static struct attribute *arm_cspmu_identifier_attrs[] = {
251 	&arm_cspmu_identifier_attr.attr,
252 	NULL,
253 };
254 
255 static struct attribute_group arm_cspmu_identifier_attr_group = {
256 	.attrs = arm_cspmu_identifier_attrs,
257 };
258 
arm_cspmu_get_identifier(const struct arm_cspmu * cspmu)259 static const char *arm_cspmu_get_identifier(const struct arm_cspmu *cspmu)
260 {
261 	const char *identifier =
262 		devm_kasprintf(cspmu->dev, GFP_KERNEL, "%x",
263 			       cspmu->impl.pmiidr);
264 	return identifier;
265 }
266 
267 static const char *arm_cspmu_type_str[ACPI_APMT_NODE_TYPE_COUNT] = {
268 	"mc",
269 	"smmu",
270 	"pcie",
271 	"acpi",
272 	"cache",
273 };
274 
arm_cspmu_get_name(const struct arm_cspmu * cspmu)275 static const char *arm_cspmu_get_name(const struct arm_cspmu *cspmu)
276 {
277 	struct device *dev;
278 	struct acpi_apmt_node *apmt_node;
279 	u8 pmu_type;
280 	char acpi_hid_string[ACPI_ID_LEN] = { 0 };
281 	static atomic_t pmu_idx;
282 	u32 id;
283 
284 	dev = cspmu->dev;
285 	apmt_node = arm_cspmu_apmt_node(dev);
286 	if (!apmt_node)
287 		return devm_kasprintf(dev, GFP_KERNEL, PMUNAME "_%u",
288 				      atomic_fetch_inc(&pmu_idx));
289 
290 	pmu_type = apmt_node->type;
291 	switch (pmu_type) {
292 	default:
293 		dev_err(dev, "unsupported PMU type-%u\n", pmu_type);
294 		return NULL;
295 	case ACPI_APMT_NODE_TYPE_ACPI:
296 		memcpy(acpi_hid_string,
297 			&apmt_node->inst_primary,
298 			sizeof(apmt_node->inst_primary));
299 		return devm_kasprintf(dev, GFP_KERNEL, "%s_%s_%s_%u", PMUNAME,
300 				      arm_cspmu_type_str[pmu_type],
301 				      acpi_hid_string,
302 				      apmt_node->inst_secondary);
303 	case ACPI_APMT_NODE_TYPE_MC:
304 		id = apmt_node->id;
305 		break;
306 	case ACPI_APMT_NODE_TYPE_SMMU:
307 	case ACPI_APMT_NODE_TYPE_PCIE_ROOT:
308 		id = apmt_node->inst_primary;
309 		break;
310 	case ACPI_APMT_NODE_TYPE_CACHE:
311 		id = apmt_node->inst_secondary;
312 		break;
313 	}
314 
315 	return devm_kasprintf(dev, GFP_KERNEL, "%s_%s_%u", PMUNAME,
316 			      arm_cspmu_type_str[pmu_type], id);
317 }
318 
arm_cspmu_cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)319 static ssize_t arm_cspmu_cpumask_show(struct device *dev,
320 				      struct device_attribute *attr,
321 				      char *buf)
322 {
323 	struct pmu *pmu = dev_get_drvdata(dev);
324 	struct arm_cspmu *cspmu = to_arm_cspmu(pmu);
325 	struct dev_ext_attribute *eattr =
326 		container_of(attr, struct dev_ext_attribute, attr);
327 	unsigned long mask_id = (unsigned long)eattr->var;
328 	const cpumask_t *cpumask;
329 
330 	switch (mask_id) {
331 	case ARM_CSPMU_ACTIVE_CPU_MASK:
332 		cpumask = &cspmu->active_cpu;
333 		break;
334 	case ARM_CSPMU_ASSOCIATED_CPU_MASK:
335 		cpumask = &cspmu->associated_cpus;
336 		break;
337 	default:
338 		return 0;
339 	}
340 	return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(cpumask));
341 }
342 
343 static struct attribute *arm_cspmu_cpumask_attrs[] = {
344 	ARM_CSPMU_CPUMASK_ATTR(cpumask, ARM_CSPMU_ACTIVE_CPU_MASK),
345 	ARM_CSPMU_CPUMASK_ATTR(associated_cpus, ARM_CSPMU_ASSOCIATED_CPU_MASK),
346 	NULL,
347 };
348 
349 static struct attribute_group arm_cspmu_cpumask_attr_group = {
350 	.attrs = arm_cspmu_cpumask_attrs,
351 };
352 
353 static struct arm_cspmu_impl_match impl_match[] = {
354 	{
355 		.module_name	= "nvidia_cspmu",
356 		.pmiidr_val	= ARM_CSPMU_IMPL_ID_NVIDIA,
357 		.pmiidr_mask	= PMIIDR_IMPLEMENTER,
358 		.module		= NULL,
359 		.impl_init_ops	= NULL,
360 	},
361 	{
362 		.module_name	= "ampere_cspmu",
363 		.pmiidr_val	= ARM_CSPMU_IMPL_ID_AMPERE,
364 		.pmiidr_mask	= PMIIDR_IMPLEMENTER,
365 		.module		= NULL,
366 		.impl_init_ops	= NULL,
367 	},
368 
369 	{0}
370 };
371 
arm_cspmu_impl_match_get(u32 pmiidr)372 static struct arm_cspmu_impl_match *arm_cspmu_impl_match_get(u32 pmiidr)
373 {
374 	struct arm_cspmu_impl_match *match = impl_match;
375 
376 	for (; match->pmiidr_val; match++) {
377 		u32 mask = match->pmiidr_mask;
378 
379 		if ((match->pmiidr_val & mask) == (pmiidr & mask))
380 			return match;
381 	}
382 
383 	return NULL;
384 }
385 
arm_cspmu_get_pmiidr(struct arm_cspmu * cspmu)386 static u32 arm_cspmu_get_pmiidr(struct arm_cspmu *cspmu)
387 {
388 	u32 pmiidr, pmpidr;
389 
390 	pmiidr = readl(cspmu->base0 + PMIIDR);
391 
392 	if (pmiidr != 0)
393 		return pmiidr;
394 
395 	/* Construct PMIIDR value from PMPIDRs. */
396 
397 	pmpidr = readl(cspmu->base0 + PMPIDR0);
398 	pmiidr |= FIELD_PREP(PMIIDR_PRODUCTID_PART_0,
399 				FIELD_GET(PMPIDR0_PART_0, pmpidr));
400 
401 	pmpidr = readl(cspmu->base0 + PMPIDR1);
402 	pmiidr |= FIELD_PREP(PMIIDR_PRODUCTID_PART_1,
403 				FIELD_GET(PMPIDR1_PART_1, pmpidr));
404 	pmiidr |= FIELD_PREP(PMIIDR_IMPLEMENTER_DES_0,
405 				FIELD_GET(PMPIDR1_DES_0, pmpidr));
406 
407 	pmpidr = readl(cspmu->base0 + PMPIDR2);
408 	pmiidr |= FIELD_PREP(PMIIDR_VARIANT,
409 				FIELD_GET(PMPIDR2_REVISION, pmpidr));
410 	pmiidr |= FIELD_PREP(PMIIDR_IMPLEMENTER_DES_1,
411 				FIELD_GET(PMPIDR2_DES_1, pmpidr));
412 
413 	pmpidr = readl(cspmu->base0 + PMPIDR3);
414 	pmiidr |= FIELD_PREP(PMIIDR_REVISION,
415 				FIELD_GET(PMPIDR3_REVAND, pmpidr));
416 
417 	pmpidr = readl(cspmu->base0 + PMPIDR4);
418 	pmiidr |= FIELD_PREP(PMIIDR_IMPLEMENTER_DES_2,
419 				FIELD_GET(PMPIDR4_DES_2, pmpidr));
420 
421 	return pmiidr;
422 }
423 
424 #define DEFAULT_IMPL_OP(name)	.name = arm_cspmu_##name
425 
arm_cspmu_init_impl_ops(struct arm_cspmu * cspmu)426 static int arm_cspmu_init_impl_ops(struct arm_cspmu *cspmu)
427 {
428 	int ret = 0;
429 	struct acpi_apmt_node *apmt_node = arm_cspmu_apmt_node(cspmu->dev);
430 	struct arm_cspmu_impl_match *match;
431 
432 	/* Start with a default PMU implementation */
433 	cspmu->impl.module = THIS_MODULE;
434 	cspmu->impl.pmiidr = arm_cspmu_get_pmiidr(cspmu);
435 	cspmu->impl.ops = (struct arm_cspmu_impl_ops) {
436 		DEFAULT_IMPL_OP(get_event_attrs),
437 		DEFAULT_IMPL_OP(get_format_attrs),
438 		DEFAULT_IMPL_OP(get_identifier),
439 		DEFAULT_IMPL_OP(get_name),
440 		DEFAULT_IMPL_OP(is_cycle_counter_event),
441 		DEFAULT_IMPL_OP(event_type),
442 		DEFAULT_IMPL_OP(set_cc_filter),
443 		DEFAULT_IMPL_OP(set_ev_filter),
444 		DEFAULT_IMPL_OP(event_attr_is_visible),
445 	};
446 
447 	/*
448 	 * With 64-bit events, since our default "cycles" encoding won't work,
449 	 * and the architecture recommends against implementing it anyway, we
450 	 * choose to effectively ignore FEAT_CSPMU_CCNTR, unless a vendor
451 	 * module really wants to provide its own encoding and ops.
452 	 */
453 	if (cspmu->has_ext64) {
454 		cspmu->impl.ops.is_cycle_counter_event = NULL;
455 		cspmu->impl.ops.set_cc_filter = NULL;
456 	}
457 
458 	/* Firmware may override implementer/product ID from PMIIDR */
459 	if (apmt_node && apmt_node->impl_id)
460 		cspmu->impl.pmiidr = apmt_node->impl_id;
461 
462 	/* Find implementer specific attribute ops. */
463 	match = arm_cspmu_impl_match_get(cspmu->impl.pmiidr);
464 
465 	/* Load implementer module and initialize the callbacks. */
466 	if (match) {
467 		mutex_lock(&arm_cspmu_lock);
468 
469 		if (match->impl_init_ops) {
470 			/* Prevent unload until PMU registration is done. */
471 			if (try_module_get(match->module)) {
472 				cspmu->impl.module = match->module;
473 				cspmu->impl.match = match;
474 				ret = match->impl_init_ops(cspmu);
475 				if (ret)
476 					module_put(match->module);
477 			} else {
478 				dev_WARN(cspmu->dev, "Failed to get module: %s\n",
479 					match->module_name);
480 				ret = -EINVAL;
481 			}
482 		} else {
483 			request_module_nowait(match->module_name);
484 			ret = dev_err_probe(cspmu->dev, -EPROBE_DEFER,
485 					    "Waiting for module %s to load\n",
486 					    match->module_name);
487 		}
488 
489 		mutex_unlock(&arm_cspmu_lock);
490 	}
491 
492 	return ret;
493 }
494 
495 static struct attribute_group *
arm_cspmu_alloc_event_attr_group(struct arm_cspmu * cspmu)496 arm_cspmu_alloc_event_attr_group(struct arm_cspmu *cspmu)
497 {
498 	struct attribute_group *event_group;
499 	struct device *dev = cspmu->dev;
500 	const struct arm_cspmu_impl_ops *impl_ops = &cspmu->impl.ops;
501 
502 	event_group =
503 		devm_kzalloc(dev, sizeof(struct attribute_group), GFP_KERNEL);
504 	if (!event_group)
505 		return NULL;
506 
507 	event_group->name = "events";
508 	event_group->is_visible = impl_ops->event_attr_is_visible;
509 	event_group->attrs = impl_ops->get_event_attrs(cspmu);
510 
511 	if (!event_group->attrs)
512 		return NULL;
513 
514 	return event_group;
515 }
516 
517 static struct attribute_group *
arm_cspmu_alloc_format_attr_group(struct arm_cspmu * cspmu)518 arm_cspmu_alloc_format_attr_group(struct arm_cspmu *cspmu)
519 {
520 	struct attribute_group *format_group;
521 	struct device *dev = cspmu->dev;
522 
523 	format_group =
524 		devm_kzalloc(dev, sizeof(struct attribute_group), GFP_KERNEL);
525 	if (!format_group)
526 		return NULL;
527 
528 	format_group->name = "format";
529 	format_group->attrs = cspmu->impl.ops.get_format_attrs(cspmu);
530 
531 	if (!format_group->attrs)
532 		return NULL;
533 
534 	return format_group;
535 }
536 
arm_cspmu_alloc_attr_groups(struct arm_cspmu * cspmu)537 static int arm_cspmu_alloc_attr_groups(struct arm_cspmu *cspmu)
538 {
539 	const struct attribute_group **attr_groups = cspmu->attr_groups;
540 	const struct arm_cspmu_impl_ops *impl_ops = &cspmu->impl.ops;
541 
542 	cspmu->identifier = impl_ops->get_identifier(cspmu);
543 	cspmu->name = impl_ops->get_name(cspmu);
544 
545 	if (!cspmu->identifier || !cspmu->name)
546 		return -ENOMEM;
547 
548 	attr_groups[0] = arm_cspmu_alloc_event_attr_group(cspmu);
549 	attr_groups[1] = arm_cspmu_alloc_format_attr_group(cspmu);
550 	attr_groups[2] = &arm_cspmu_identifier_attr_group;
551 	attr_groups[3] = &arm_cspmu_cpumask_attr_group;
552 
553 	if (!attr_groups[0] || !attr_groups[1])
554 		return -ENOMEM;
555 
556 	return 0;
557 }
558 
arm_cspmu_pmcr(struct arm_cspmu * cspmu)559 static inline int arm_cspmu_pmcr(struct arm_cspmu *cspmu)
560 {
561 	return cspmu->has_ext64 ? PMCR_64 : PMCR;
562 }
563 
arm_cspmu_reset_counters(struct arm_cspmu * cspmu)564 static inline void arm_cspmu_reset_counters(struct arm_cspmu *cspmu)
565 {
566 	writel(PMCR_C | PMCR_P, cspmu->base0 + arm_cspmu_pmcr(cspmu));
567 }
568 
arm_cspmu_start_counters(struct arm_cspmu * cspmu)569 static inline void arm_cspmu_start_counters(struct arm_cspmu *cspmu)
570 {
571 	writel(PMCR_E, cspmu->base0 + arm_cspmu_pmcr(cspmu));
572 }
573 
arm_cspmu_stop_counters(struct arm_cspmu * cspmu)574 static inline void arm_cspmu_stop_counters(struct arm_cspmu *cspmu)
575 {
576 	writel(0, cspmu->base0 + arm_cspmu_pmcr(cspmu));
577 }
578 
arm_cspmu_enable(struct pmu * pmu)579 static void arm_cspmu_enable(struct pmu *pmu)
580 {
581 	bool disabled;
582 	struct arm_cspmu *cspmu = to_arm_cspmu(pmu);
583 
584 	disabled = bitmap_empty(cspmu->hw_events.used_ctrs,
585 				cspmu->num_logical_ctrs);
586 
587 	if (disabled)
588 		return;
589 
590 	arm_cspmu_start_counters(cspmu);
591 }
592 
arm_cspmu_disable(struct pmu * pmu)593 static void arm_cspmu_disable(struct pmu *pmu)
594 {
595 	struct arm_cspmu *cspmu = to_arm_cspmu(pmu);
596 
597 	arm_cspmu_stop_counters(cspmu);
598 }
599 
arm_cspmu_get_event_idx(struct arm_cspmu_hw_events * hw_events,struct perf_event * event)600 static int arm_cspmu_get_event_idx(struct arm_cspmu_hw_events *hw_events,
601 				struct perf_event *event)
602 {
603 	int idx, ret;
604 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
605 
606 	if (supports_cycle_counter(cspmu)) {
607 		if (cspmu->impl.ops.is_cycle_counter_event &&
608 		    cspmu->impl.ops.is_cycle_counter_event(event)) {
609 			/* Search for available cycle counter. */
610 			if (test_and_set_bit(cspmu->cycle_counter_logical_idx,
611 					     hw_events->used_ctrs))
612 				return -EAGAIN;
613 
614 			return cspmu->cycle_counter_logical_idx;
615 		}
616 
617 		/*
618 		 * Search a regular counter from the used counter bitmap.
619 		 * The cycle counter divides the bitmap into two parts. Search
620 		 * the first then second half to exclude the cycle counter bit.
621 		 */
622 		idx = find_first_zero_bit(hw_events->used_ctrs,
623 					  cspmu->cycle_counter_logical_idx);
624 		if (idx >= cspmu->cycle_counter_logical_idx) {
625 			idx = find_next_zero_bit(
626 				hw_events->used_ctrs,
627 				cspmu->num_logical_ctrs,
628 				cspmu->cycle_counter_logical_idx + 1);
629 		}
630 	} else {
631 		idx = find_first_zero_bit(hw_events->used_ctrs,
632 					  cspmu->num_logical_ctrs);
633 	}
634 
635 	if (idx >= cspmu->num_logical_ctrs)
636 		return -EAGAIN;
637 
638 	if (cspmu->impl.ops.validate_event) {
639 		ret = cspmu->impl.ops.validate_event(cspmu, event);
640 		if (ret)
641 			return ret;
642 	}
643 
644 	set_bit(idx, hw_events->used_ctrs);
645 
646 	return idx;
647 }
648 
arm_cspmu_validate_event(struct pmu * pmu,struct arm_cspmu_hw_events * hw_events,struct perf_event * event)649 static bool arm_cspmu_validate_event(struct pmu *pmu,
650 				 struct arm_cspmu_hw_events *hw_events,
651 				 struct perf_event *event)
652 {
653 	if (is_software_event(event))
654 		return true;
655 
656 	/* Reject groups spanning multiple HW PMUs. */
657 	if (event->pmu != pmu)
658 		return false;
659 
660 	return (arm_cspmu_get_event_idx(hw_events, event) >= 0);
661 }
662 
663 /*
664  * Make sure the group of events can be scheduled at once
665  * on the PMU.
666  */
arm_cspmu_validate_group(struct perf_event * event)667 static bool arm_cspmu_validate_group(struct perf_event *event)
668 {
669 	struct perf_event *sibling, *leader = event->group_leader;
670 	struct arm_cspmu_hw_events fake_hw_events;
671 
672 	if (event->group_leader == event)
673 		return true;
674 
675 	memset(&fake_hw_events, 0, sizeof(fake_hw_events));
676 
677 	if (!arm_cspmu_validate_event(event->pmu, &fake_hw_events, leader))
678 		return false;
679 
680 	for_each_sibling_event(sibling, leader) {
681 		if (!arm_cspmu_validate_event(event->pmu, &fake_hw_events,
682 						  sibling))
683 			return false;
684 	}
685 
686 	return arm_cspmu_validate_event(event->pmu, &fake_hw_events, event);
687 }
688 
arm_cspmu_event_init(struct perf_event * event)689 static int arm_cspmu_event_init(struct perf_event *event)
690 {
691 	struct arm_cspmu *cspmu;
692 	struct hw_perf_event *hwc = &event->hw;
693 
694 	cspmu = to_arm_cspmu(event->pmu);
695 
696 	if (event->attr.type != event->pmu->type)
697 		return -ENOENT;
698 
699 	/*
700 	 * Following other "uncore" PMUs, we do not support sampling mode or
701 	 * attach to a task (per-process mode).
702 	 */
703 	if (is_sampling_event(event)) {
704 		dev_dbg(cspmu->pmu.dev,
705 			"Can't support sampling events\n");
706 		return -EOPNOTSUPP;
707 	}
708 
709 	if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK) {
710 		dev_dbg(cspmu->pmu.dev,
711 			"Can't support per-task counters\n");
712 		return -EINVAL;
713 	}
714 
715 	/*
716 	 * Make sure the CPU assignment is on one of the CPUs associated with
717 	 * this PMU.
718 	 */
719 	if (!cpumask_test_cpu(event->cpu, &cspmu->associated_cpus)) {
720 		dev_dbg(cspmu->pmu.dev,
721 			"Requested cpu is not associated with the PMU\n");
722 		return -EINVAL;
723 	}
724 
725 	/* Enforce the current active CPU to handle the events in this PMU. */
726 	event->cpu = cpumask_first(&cspmu->active_cpu);
727 	if (event->cpu >= nr_cpu_ids)
728 		return -EINVAL;
729 
730 	if (!arm_cspmu_validate_group(event))
731 		return -EINVAL;
732 
733 	/*
734 	 * The logical counter id is tracked with hw_perf_event.extra_reg.idx.
735 	 * The physical counter id is tracked with hw_perf_event.idx.
736 	 * We don't assign an index until we actually place the event onto
737 	 * hardware. Use -1 to signify that we haven't decided where to put it
738 	 * yet.
739 	 */
740 	hwc->idx = -1;
741 	hwc->extra_reg.idx = -1;
742 	hwc->config = cspmu->impl.ops.event_type(event);
743 
744 	return 0;
745 }
746 
counter_offset(u32 reg_sz,u32 ctr_idx)747 static inline u32 counter_offset(u32 reg_sz, u32 ctr_idx)
748 {
749 	return (PMEVCNTR_LO + (reg_sz * ctr_idx));
750 }
751 
arm_cspmu_write_counter(struct perf_event * event,u64 val)752 static void arm_cspmu_write_counter(struct perf_event *event, u64 val)
753 {
754 	u32 offset;
755 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
756 
757 	if (use_64b_counter_reg(cspmu)) {
758 		offset = counter_offset(sizeof(u64), event->hw.idx);
759 
760 		if (cspmu->has_atomic_dword)
761 			writeq(val, cspmu->base1 + offset);
762 		else
763 			lo_hi_writeq(val, cspmu->base1 + offset);
764 	} else {
765 		offset = counter_offset(sizeof(u32), event->hw.idx);
766 
767 		writel(lower_32_bits(val), cspmu->base1 + offset);
768 	}
769 }
770 
arm_cspmu_read_counter(struct perf_event * event)771 static u64 arm_cspmu_read_counter(struct perf_event *event)
772 {
773 	u32 offset;
774 	const void __iomem *counter_addr;
775 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
776 
777 	if (use_64b_counter_reg(cspmu)) {
778 		offset = counter_offset(sizeof(u64), event->hw.idx);
779 		counter_addr = cspmu->base1 + offset;
780 
781 		return cspmu->has_atomic_dword ?
782 			       readq(counter_addr) :
783 			       read_reg64_hilohi(counter_addr, HILOHI_MAX_POLL);
784 	}
785 
786 	offset = counter_offset(sizeof(u32), event->hw.idx);
787 	return readl(cspmu->base1 + offset);
788 }
789 
790 /*
791  * arm_cspmu_set_event_period: Set the period for the counter.
792  *
793  * To handle cases of extreme interrupt latency, we program
794  * the counter with half of the max count for the counters.
795  */
arm_cspmu_set_event_period(struct perf_event * event)796 static void arm_cspmu_set_event_period(struct perf_event *event)
797 {
798 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
799 	u64 val = counter_mask(cspmu) >> 1ULL;
800 
801 	local64_set(&event->hw.prev_count, val);
802 	arm_cspmu_write_counter(event, val);
803 }
804 
arm_cspmu_enable_counter(struct arm_cspmu * cspmu,int idx)805 static void arm_cspmu_enable_counter(struct arm_cspmu *cspmu, int idx)
806 {
807 	u32 reg_id, reg_bit, inten_off, cnten_off;
808 
809 	reg_id = COUNTER_TO_SET_CLR_ID(idx);
810 	reg_bit = COUNTER_TO_SET_CLR_BIT(idx);
811 
812 	inten_off = PMINTENSET + (4 * reg_id);
813 	cnten_off = PMCNTENSET + (4 * reg_id);
814 
815 	writel(BIT(reg_bit), cspmu->base0 + inten_off);
816 	writel(BIT(reg_bit), cspmu->base0 + cnten_off);
817 }
818 
arm_cspmu_disable_counter(struct arm_cspmu * cspmu,int idx)819 static void arm_cspmu_disable_counter(struct arm_cspmu *cspmu, int idx)
820 {
821 	u32 reg_id, reg_bit, inten_off, cnten_off;
822 
823 	reg_id = COUNTER_TO_SET_CLR_ID(idx);
824 	reg_bit = COUNTER_TO_SET_CLR_BIT(idx);
825 
826 	inten_off = PMINTENCLR + (4 * reg_id);
827 	cnten_off = PMCNTENCLR + (4 * reg_id);
828 
829 	writel(BIT(reg_bit), cspmu->base0 + cnten_off);
830 	writel(BIT(reg_bit), cspmu->base0 + inten_off);
831 }
832 
arm_cspmu_event_update(struct perf_event * event)833 static void arm_cspmu_event_update(struct perf_event *event)
834 {
835 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
836 	struct hw_perf_event *hwc = &event->hw;
837 	u64 delta, prev, now;
838 
839 	do {
840 		prev = local64_read(&hwc->prev_count);
841 		now = arm_cspmu_read_counter(event);
842 	} while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
843 
844 	delta = (now - prev) & counter_mask(cspmu);
845 	local64_add(delta, &event->count);
846 }
847 
arm_cspmu_set_event(struct arm_cspmu * cspmu,struct hw_perf_event * hwc)848 static inline void arm_cspmu_set_event(struct arm_cspmu *cspmu,
849 					struct hw_perf_event *hwc)
850 {
851 	if (cspmu->has_ext64)
852 		writeq(hwc->config, cspmu->base0 + PMEVTYPER + (8 * hwc->idx));
853 	else
854 		writel(hwc->config, cspmu->base0 + PMEVTYPER + (4 * hwc->idx));
855 }
856 
arm_cspmu_set_ev_filter(struct arm_cspmu * cspmu,const struct perf_event * event)857 static void arm_cspmu_set_ev_filter(struct arm_cspmu *cspmu,
858 				    const struct perf_event *event)
859 {
860 	u64 filter = arm_cspmu_filter(event);
861 	u64 filter2 = arm_cspmu_filter2(event);
862 	int n = event->hw.idx;
863 
864 	if (cspmu->has_ext64) {
865 		writeq(filter, cspmu->base0 + PMEVFILTR + (8 * n));
866 		writeq(filter2, cspmu->base0 + PMEVFILT2R + (8 * n));
867 	} else {
868 		writel(filter, cspmu->base0 + PMEVFILTR + (4 * n));
869 		writel(filter2, cspmu->base0 + PMEVFILT2R + (4 * n));
870 	}
871 }
872 
873 /* Note we deliberately don't expect 64-bit filters here; see init_impl_ops */
arm_cspmu_set_cc_filter(struct arm_cspmu * cspmu,const struct perf_event * event)874 static void arm_cspmu_set_cc_filter(struct arm_cspmu *cspmu,
875 				    const struct perf_event *event)
876 {
877 	u32 filter = arm_cspmu_filter(event);
878 
879 	writel(filter, cspmu->base0 + PMCCFILTR);
880 }
881 
arm_cspmu_start(struct perf_event * event,int pmu_flags)882 static void arm_cspmu_start(struct perf_event *event, int pmu_flags)
883 {
884 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
885 	struct hw_perf_event *hwc = &event->hw;
886 
887 	/* We always reprogram the counter */
888 	if (pmu_flags & PERF_EF_RELOAD)
889 		WARN_ON(!(hwc->state & PERF_HES_UPTODATE));
890 
891 	arm_cspmu_set_event_period(event);
892 
893 	if (event->hw.extra_reg.idx == cspmu->cycle_counter_logical_idx) {
894 		cspmu->impl.ops.set_cc_filter(cspmu, event);
895 	} else {
896 		arm_cspmu_set_event(cspmu, hwc);
897 		cspmu->impl.ops.set_ev_filter(cspmu, event);
898 	}
899 
900 	hwc->state = 0;
901 
902 	arm_cspmu_enable_counter(cspmu, hwc->idx);
903 }
904 
arm_cspmu_stop(struct perf_event * event,int pmu_flags)905 static void arm_cspmu_stop(struct perf_event *event, int pmu_flags)
906 {
907 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
908 	struct hw_perf_event *hwc = &event->hw;
909 
910 	if (hwc->state & PERF_HES_STOPPED)
911 		return;
912 
913 	arm_cspmu_disable_counter(cspmu, hwc->idx);
914 
915 	if (cspmu->impl.ops.reset_ev_filter)
916 		cspmu->impl.ops.reset_ev_filter(cspmu, event);
917 
918 	arm_cspmu_event_update(event);
919 
920 	hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
921 }
922 
to_phys_idx(struct arm_cspmu * cspmu,u32 idx)923 static inline u32 to_phys_idx(struct arm_cspmu *cspmu, u32 idx)
924 {
925 	return (idx == cspmu->cycle_counter_logical_idx) ?
926 		ARM_CSPMU_CYCLE_CNTR_IDX : idx;
927 }
928 
arm_cspmu_add(struct perf_event * event,int flags)929 static int arm_cspmu_add(struct perf_event *event, int flags)
930 {
931 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
932 	struct arm_cspmu_hw_events *hw_events = &cspmu->hw_events;
933 	struct hw_perf_event *hwc = &event->hw;
934 	int idx;
935 
936 	if (WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(),
937 					   &cspmu->associated_cpus)))
938 		return -ENOENT;
939 
940 	idx = arm_cspmu_get_event_idx(hw_events, event);
941 	if (idx < 0)
942 		return idx;
943 
944 	hw_events->events[idx] = event;
945 	hwc->idx = to_phys_idx(cspmu, idx);
946 	hwc->extra_reg.idx = idx;
947 	hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
948 
949 	if (flags & PERF_EF_START)
950 		arm_cspmu_start(event, PERF_EF_RELOAD);
951 
952 	/* Propagate changes to the userspace mapping. */
953 	perf_event_update_userpage(event);
954 
955 	return 0;
956 }
957 
arm_cspmu_del(struct perf_event * event,int flags)958 static void arm_cspmu_del(struct perf_event *event, int flags)
959 {
960 	struct arm_cspmu *cspmu = to_arm_cspmu(event->pmu);
961 	struct arm_cspmu_hw_events *hw_events = &cspmu->hw_events;
962 	struct hw_perf_event *hwc = &event->hw;
963 	int idx = hwc->extra_reg.idx;
964 
965 	arm_cspmu_stop(event, PERF_EF_UPDATE);
966 
967 	hw_events->events[idx] = NULL;
968 
969 	clear_bit(idx, hw_events->used_ctrs);
970 
971 	perf_event_update_userpage(event);
972 }
973 
arm_cspmu_read(struct perf_event * event)974 static void arm_cspmu_read(struct perf_event *event)
975 {
976 	arm_cspmu_event_update(event);
977 }
978 
arm_cspmu_alloc(struct platform_device * pdev)979 static struct arm_cspmu *arm_cspmu_alloc(struct platform_device *pdev)
980 {
981 	struct acpi_apmt_node *apmt_node;
982 	struct arm_cspmu *cspmu;
983 	struct device *dev = &pdev->dev;
984 
985 	cspmu = devm_kzalloc(dev, sizeof(*cspmu), GFP_KERNEL);
986 	if (!cspmu)
987 		return NULL;
988 
989 	cspmu->dev = dev;
990 	platform_set_drvdata(pdev, cspmu);
991 
992 	apmt_node = arm_cspmu_apmt_node(dev);
993 	if (apmt_node) {
994 		cspmu->has_atomic_dword = apmt_node->flags & ACPI_APMT_FLAGS_ATOMIC;
995 	} else {
996 		u32 width = 0;
997 
998 		device_property_read_u32(dev, "reg-io-width", &width);
999 		cspmu->has_atomic_dword = (width == 8);
1000 	}
1001 
1002 	return cspmu;
1003 }
1004 
arm_cspmu_init_mmio(struct arm_cspmu * cspmu)1005 static int arm_cspmu_init_mmio(struct arm_cspmu *cspmu)
1006 {
1007 	struct device *dev;
1008 	struct platform_device *pdev;
1009 
1010 	dev = cspmu->dev;
1011 	pdev = to_platform_device(dev);
1012 
1013 	/* Base address for page 0. */
1014 	cspmu->base0 = devm_platform_ioremap_resource(pdev, 0);
1015 	if (IS_ERR(cspmu->base0)) {
1016 		dev_err(dev, "ioremap failed for page-0 resource\n");
1017 		return PTR_ERR(cspmu->base0);
1018 	}
1019 
1020 	/* Base address for page 1 if supported. Otherwise point to page 0. */
1021 	cspmu->base1 = cspmu->base0;
1022 	if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
1023 		cspmu->base1 = devm_platform_ioremap_resource(pdev, 1);
1024 		if (IS_ERR(cspmu->base1)) {
1025 			dev_err(dev, "ioremap failed for page-1 resource\n");
1026 			return PTR_ERR(cspmu->base1);
1027 		}
1028 	}
1029 
1030 	/*
1031 	 * We can infer FEAT_CSPMU_EXT64 from PMCNTEN, or hope that anything
1032 	 * that failed to get that right has at least implemented the optional
1033 	 * PMDEVARCH correctly...
1034 	 *
1035 	 * Note that architecturally, has_ext64 *should* imply has_atomic_dword,
1036 	 * but enough implementations have ignored that already that we'll just
1037 	 * have to still rely on the firmware flag.
1038 	 */
1039 	writel(~0U, cspmu->base0 + PMCNTENCLR);
1040 	writel(~0U, cspmu->base0 + PMCNTEN);
1041 	if (readl(cspmu->base0 + PMCNTENCLR)) {
1042 		cspmu->has_ext64 = true;
1043 		writel(0, cspmu->base0 + PMCNTEN);
1044 	} else {
1045 		u32 reg = readl(cspmu->base0 + PMDEVARCH);
1046 
1047 		if (reg & ARM_CSPMU_PMDEVARCH_PRESENT) {
1048 			reg &= ARM_CSPMU_PMDEVARCH_ARCHPART;
1049 			if (reg == 0xaf4 || reg == 0xaf5)
1050 				cspmu->has_ext64 = true;
1051 		}
1052 	}
1053 
1054 	cspmu->pmcfgr = readl(cspmu->base0 + PMCFGR);
1055 
1056 	cspmu->num_logical_ctrs = FIELD_GET(PMCFGR_N, cspmu->pmcfgr) + 1;
1057 
1058 	cspmu->cycle_counter_logical_idx = ARM_CSPMU_MAX_HW_CNTRS;
1059 
1060 	if (supports_cycle_counter(cspmu)) {
1061 		/*
1062 		 * The last logical counter is mapped to cycle counter if
1063 		 * there is a gap between regular and cycle counter. Otherwise,
1064 		 * logical and physical have 1-to-1 mapping.
1065 		 */
1066 		cspmu->cycle_counter_logical_idx =
1067 			(cspmu->num_logical_ctrs <= ARM_CSPMU_CYCLE_CNTR_IDX) ?
1068 				cspmu->num_logical_ctrs - 1 :
1069 				ARM_CSPMU_CYCLE_CNTR_IDX;
1070 	}
1071 
1072 	cspmu->num_set_clr_reg =
1073 		DIV_ROUND_UP(cspmu->num_logical_ctrs,
1074 				ARM_CSPMU_SET_CLR_COUNTER_NUM);
1075 
1076 	cspmu->hw_events.events =
1077 		devm_kcalloc(dev, cspmu->num_logical_ctrs,
1078 			     sizeof(*cspmu->hw_events.events), GFP_KERNEL);
1079 
1080 	if (!cspmu->hw_events.events)
1081 		return -ENOMEM;
1082 
1083 	return 0;
1084 }
1085 
arm_cspmu_get_reset_overflow(struct arm_cspmu * cspmu,u32 * pmovs)1086 static inline int arm_cspmu_get_reset_overflow(struct arm_cspmu *cspmu,
1087 					       u32 *pmovs)
1088 {
1089 	int i;
1090 	u32 pmovclr_offset = PMOVSCLR;
1091 	u32 has_overflowed = 0;
1092 
1093 	for (i = 0; i < cspmu->num_set_clr_reg; ++i) {
1094 		pmovs[i] = readl(cspmu->base1 + pmovclr_offset);
1095 		has_overflowed |= pmovs[i];
1096 		writel(pmovs[i], cspmu->base1 + pmovclr_offset);
1097 		pmovclr_offset += sizeof(u32);
1098 	}
1099 
1100 	return has_overflowed != 0;
1101 }
1102 
arm_cspmu_handle_irq(int irq_num,void * dev)1103 static irqreturn_t arm_cspmu_handle_irq(int irq_num, void *dev)
1104 {
1105 	int idx, has_overflowed;
1106 	struct perf_event *event;
1107 	struct arm_cspmu *cspmu = dev;
1108 	DECLARE_BITMAP(pmovs, ARM_CSPMU_MAX_HW_CNTRS);
1109 	bool handled = false;
1110 
1111 	arm_cspmu_stop_counters(cspmu);
1112 
1113 	has_overflowed = arm_cspmu_get_reset_overflow(cspmu, (u32 *)pmovs);
1114 	if (!has_overflowed)
1115 		goto done;
1116 
1117 	for_each_set_bit(idx, cspmu->hw_events.used_ctrs,
1118 			cspmu->num_logical_ctrs) {
1119 		event = cspmu->hw_events.events[idx];
1120 
1121 		if (!event)
1122 			continue;
1123 
1124 		if (!test_bit(event->hw.idx, pmovs))
1125 			continue;
1126 
1127 		arm_cspmu_event_update(event);
1128 		arm_cspmu_set_event_period(event);
1129 
1130 		handled = true;
1131 	}
1132 
1133 done:
1134 	arm_cspmu_start_counters(cspmu);
1135 	return IRQ_RETVAL(handled);
1136 }
1137 
arm_cspmu_request_irq(struct arm_cspmu * cspmu)1138 static int arm_cspmu_request_irq(struct arm_cspmu *cspmu)
1139 {
1140 	int irq, ret;
1141 	struct device *dev;
1142 	struct platform_device *pdev;
1143 
1144 	dev = cspmu->dev;
1145 	pdev = to_platform_device(dev);
1146 
1147 	/* Skip IRQ request if the PMU does not support overflow interrupt. */
1148 	irq = platform_get_irq_optional(pdev, 0);
1149 	if (irq < 0)
1150 		return irq == -ENXIO ? 0 : irq;
1151 
1152 	ret = devm_request_irq(dev, irq, arm_cspmu_handle_irq,
1153 			       IRQF_NOBALANCING | IRQF_NO_THREAD, dev_name(dev),
1154 			       cspmu);
1155 	if (ret)
1156 		return ret;
1157 
1158 	cspmu->irq = irq;
1159 
1160 	return 0;
1161 }
1162 
1163 #if defined(CONFIG_ACPI) && defined(CONFIG_ARM64)
1164 #include <acpi/processor.h>
1165 
arm_cspmu_find_cpu_container(int cpu,u32 container_uid)1166 static inline int arm_cspmu_find_cpu_container(int cpu, u32 container_uid)
1167 {
1168 	struct device *cpu_dev;
1169 	struct acpi_device *acpi_dev;
1170 
1171 	cpu_dev = get_cpu_device(cpu);
1172 	if (!cpu_dev)
1173 		return -ENODEV;
1174 
1175 	acpi_dev = ACPI_COMPANION(cpu_dev);
1176 	while (acpi_dev) {
1177 		if (acpi_dev_hid_uid_match(acpi_dev, ACPI_PROCESSOR_CONTAINER_HID, container_uid))
1178 			return 0;
1179 
1180 		acpi_dev = acpi_dev_parent(acpi_dev);
1181 	}
1182 
1183 	return -ENODEV;
1184 }
1185 
arm_cspmu_acpi_get_cpus(struct arm_cspmu * cspmu)1186 static int arm_cspmu_acpi_get_cpus(struct arm_cspmu *cspmu)
1187 {
1188 	struct acpi_apmt_node *apmt_node;
1189 	int affinity_flag;
1190 	u32 cpu_uid;
1191 	int cpu;
1192 	int ret;
1193 
1194 	apmt_node = arm_cspmu_apmt_node(cspmu->dev);
1195 	affinity_flag = apmt_node->flags & ACPI_APMT_FLAGS_AFFINITY;
1196 
1197 	if (affinity_flag == ACPI_APMT_FLAGS_AFFINITY_PROC) {
1198 		for_each_possible_cpu(cpu) {
1199 			ret = acpi_get_cpu_uid(cpu, &cpu_uid);
1200 			if (ret == 0 && apmt_node->proc_affinity == cpu_uid) {
1201 				cpumask_set_cpu(cpu, &cspmu->associated_cpus);
1202 				break;
1203 			}
1204 		}
1205 	} else {
1206 		for_each_possible_cpu(cpu) {
1207 			if (arm_cspmu_find_cpu_container(
1208 				    cpu, apmt_node->proc_affinity))
1209 				continue;
1210 
1211 			cpumask_set_cpu(cpu, &cspmu->associated_cpus);
1212 		}
1213 	}
1214 
1215 	return 0;
1216 }
1217 
arm_cspmu_acpi_dev_get(const struct arm_cspmu * cspmu)1218 struct acpi_device *arm_cspmu_acpi_dev_get(const struct arm_cspmu *cspmu)
1219 {
1220 	char hid[16] = {};
1221 	char uid[16] = {};
1222 	const struct acpi_apmt_node *apmt_node;
1223 
1224 	apmt_node = arm_cspmu_apmt_node(cspmu->dev);
1225 	if (!apmt_node || apmt_node->type != ACPI_APMT_NODE_TYPE_ACPI)
1226 		return NULL;
1227 
1228 	memcpy(hid, &apmt_node->inst_primary, sizeof(apmt_node->inst_primary));
1229 	snprintf(uid, sizeof(uid), "%u", apmt_node->inst_secondary);
1230 
1231 	return acpi_dev_get_first_match_dev(hid, uid, -1);
1232 }
1233 EXPORT_SYMBOL_GPL(arm_cspmu_acpi_dev_get);
1234 #else
arm_cspmu_acpi_get_cpus(struct arm_cspmu * cspmu)1235 static int arm_cspmu_acpi_get_cpus(struct arm_cspmu *cspmu)
1236 {
1237 	return -ENODEV;
1238 }
1239 #endif
1240 
arm_cspmu_of_get_cpus(struct arm_cspmu * cspmu)1241 static int arm_cspmu_of_get_cpus(struct arm_cspmu *cspmu)
1242 {
1243 	struct of_phandle_iterator it;
1244 	int ret, cpu;
1245 
1246 	of_for_each_phandle(&it, ret, dev_of_node(cspmu->dev), "cpus", NULL, 0) {
1247 		cpu = of_cpu_node_to_id(it.node);
1248 		if (cpu < 0)
1249 			continue;
1250 		cpumask_set_cpu(cpu, &cspmu->associated_cpus);
1251 	}
1252 	return ret == -ENOENT ? 0 : ret;
1253 }
1254 
arm_cspmu_get_cpus(struct arm_cspmu * cspmu)1255 static int arm_cspmu_get_cpus(struct arm_cspmu *cspmu)
1256 {
1257 	int ret = 0;
1258 
1259 	if (arm_cspmu_apmt_node(cspmu->dev))
1260 		ret = arm_cspmu_acpi_get_cpus(cspmu);
1261 	else if (device_property_present(cspmu->dev, "cpus"))
1262 		ret = arm_cspmu_of_get_cpus(cspmu);
1263 	else
1264 		cpumask_copy(&cspmu->associated_cpus, cpu_possible_mask);
1265 
1266 	if (!ret && cpumask_empty(&cspmu->associated_cpus)) {
1267 		dev_dbg(cspmu->dev, "No cpu associated with the PMU\n");
1268 		ret = -ENODEV;
1269 	}
1270 	return ret;
1271 }
1272 
arm_cspmu_register_pmu(struct arm_cspmu * cspmu)1273 static int arm_cspmu_register_pmu(struct arm_cspmu *cspmu)
1274 {
1275 	int ret, capabilities;
1276 
1277 	ret = arm_cspmu_alloc_attr_groups(cspmu);
1278 	if (ret)
1279 		return ret;
1280 
1281 	ret = cpuhp_state_add_instance(arm_cspmu_cpuhp_state,
1282 				       &cspmu->cpuhp_node);
1283 	if (ret)
1284 		return ret;
1285 
1286 	capabilities = PERF_PMU_CAP_NO_EXCLUDE;
1287 	if (cspmu->irq == 0)
1288 		capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
1289 
1290 	cspmu->pmu = (struct pmu){
1291 		.task_ctx_nr	= perf_invalid_context,
1292 		.module		= cspmu->impl.module,
1293 		.parent		= cspmu->dev,
1294 		.pmu_enable	= arm_cspmu_enable,
1295 		.pmu_disable	= arm_cspmu_disable,
1296 		.event_init	= arm_cspmu_event_init,
1297 		.add		= arm_cspmu_add,
1298 		.del		= arm_cspmu_del,
1299 		.start		= arm_cspmu_start,
1300 		.stop		= arm_cspmu_stop,
1301 		.read		= arm_cspmu_read,
1302 		.attr_groups	= cspmu->attr_groups,
1303 		.capabilities	= capabilities,
1304 	};
1305 
1306 	/* Hardware counter init */
1307 	arm_cspmu_reset_counters(cspmu);
1308 
1309 	ret = perf_pmu_register(&cspmu->pmu, cspmu->name, -1);
1310 	if (ret) {
1311 		cpuhp_state_remove_instance(arm_cspmu_cpuhp_state,
1312 					    &cspmu->cpuhp_node);
1313 	}
1314 
1315 	return ret;
1316 }
1317 
arm_cspmu_device_probe(struct platform_device * pdev)1318 static int arm_cspmu_device_probe(struct platform_device *pdev)
1319 {
1320 	int ret;
1321 	struct arm_cspmu *cspmu;
1322 
1323 	cspmu = arm_cspmu_alloc(pdev);
1324 	if (!cspmu)
1325 		return -ENOMEM;
1326 
1327 	ret = arm_cspmu_init_mmio(cspmu);
1328 	if (ret)
1329 		return ret;
1330 
1331 	ret = arm_cspmu_request_irq(cspmu);
1332 	if (ret) {
1333 		if (counter_size(cspmu) < 64)
1334 			return ret;
1335 		dev_info(cspmu->dev, "Continuing without IRQ\n");
1336 	}
1337 
1338 	ret = arm_cspmu_get_cpus(cspmu);
1339 	if (ret)
1340 		return ret;
1341 
1342 	ret = arm_cspmu_init_impl_ops(cspmu);
1343 	if (ret)
1344 		return ret;
1345 
1346 	ret = arm_cspmu_register_pmu(cspmu);
1347 
1348 	/* Matches arm_cspmu_init_impl_ops() above. */
1349 	if (cspmu->impl.module != THIS_MODULE)
1350 		module_put(cspmu->impl.module);
1351 
1352 	return ret;
1353 }
1354 
arm_cspmu_device_remove(struct platform_device * pdev)1355 static void arm_cspmu_device_remove(struct platform_device *pdev)
1356 {
1357 	struct arm_cspmu *cspmu = platform_get_drvdata(pdev);
1358 
1359 	perf_pmu_unregister(&cspmu->pmu);
1360 	cpuhp_state_remove_instance(arm_cspmu_cpuhp_state, &cspmu->cpuhp_node);
1361 }
1362 
1363 static const struct platform_device_id arm_cspmu_id[] = {
1364 	{DRVNAME, 0},
1365 	{ },
1366 };
1367 MODULE_DEVICE_TABLE(platform, arm_cspmu_id);
1368 
1369 static const struct of_device_id arm_cspmu_of_match[] = {
1370 	{ .compatible = "arm,coresight-pmu" },
1371 	{}
1372 };
1373 MODULE_DEVICE_TABLE(of, arm_cspmu_of_match);
1374 
1375 static struct platform_driver arm_cspmu_driver = {
1376 	.driver = {
1377 		.name = DRVNAME,
1378 		.of_match_table = arm_cspmu_of_match,
1379 		.suppress_bind_attrs = true,
1380 	},
1381 	.probe = arm_cspmu_device_probe,
1382 	.remove = arm_cspmu_device_remove,
1383 	.id_table = arm_cspmu_id,
1384 };
1385 
arm_cspmu_set_active_cpu(int cpu,struct arm_cspmu * cspmu)1386 static void arm_cspmu_set_active_cpu(int cpu, struct arm_cspmu *cspmu)
1387 {
1388 	cpumask_set_cpu(cpu, &cspmu->active_cpu);
1389 	if (cspmu->irq)
1390 		WARN_ON(irq_set_affinity(cspmu->irq, &cspmu->active_cpu));
1391 }
1392 
arm_cspmu_cpu_online(unsigned int cpu,struct hlist_node * node)1393 static int arm_cspmu_cpu_online(unsigned int cpu, struct hlist_node *node)
1394 {
1395 	struct arm_cspmu *cspmu =
1396 		hlist_entry_safe(node, struct arm_cspmu, cpuhp_node);
1397 
1398 	if (!cpumask_test_cpu(cpu, &cspmu->associated_cpus))
1399 		return 0;
1400 
1401 	/* If the PMU is already managed, there is nothing to do */
1402 	if (!cpumask_empty(&cspmu->active_cpu))
1403 		return 0;
1404 
1405 	/* Use this CPU for event counting */
1406 	arm_cspmu_set_active_cpu(cpu, cspmu);
1407 
1408 	return 0;
1409 }
1410 
arm_cspmu_cpu_teardown(unsigned int cpu,struct hlist_node * node)1411 static int arm_cspmu_cpu_teardown(unsigned int cpu, struct hlist_node *node)
1412 {
1413 	unsigned int dst;
1414 
1415 	struct arm_cspmu *cspmu =
1416 		hlist_entry_safe(node, struct arm_cspmu, cpuhp_node);
1417 
1418 	/* Nothing to do if this CPU doesn't own the PMU */
1419 	if (!cpumask_test_and_clear_cpu(cpu, &cspmu->active_cpu))
1420 		return 0;
1421 
1422 	/* Choose a new CPU to migrate ownership of the PMU to */
1423 	dst = cpumask_any_and_but(&cspmu->associated_cpus,
1424 				  cpu_online_mask, cpu);
1425 	if (dst >= nr_cpu_ids)
1426 		return 0;
1427 
1428 	/* Use this CPU for event counting */
1429 	perf_pmu_migrate_context(&cspmu->pmu, cpu, dst);
1430 	arm_cspmu_set_active_cpu(dst, cspmu);
1431 
1432 	return 0;
1433 }
1434 
arm_cspmu_init(void)1435 static int __init arm_cspmu_init(void)
1436 {
1437 	int ret;
1438 
1439 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
1440 					"perf/arm/cspmu:online",
1441 					arm_cspmu_cpu_online,
1442 					arm_cspmu_cpu_teardown);
1443 	if (ret < 0)
1444 		return ret;
1445 	arm_cspmu_cpuhp_state = ret;
1446 	return platform_driver_register(&arm_cspmu_driver);
1447 }
1448 
arm_cspmu_exit(void)1449 static void __exit arm_cspmu_exit(void)
1450 {
1451 	platform_driver_unregister(&arm_cspmu_driver);
1452 	cpuhp_remove_multi_state(arm_cspmu_cpuhp_state);
1453 }
1454 
arm_cspmu_impl_register(const struct arm_cspmu_impl_match * impl_match)1455 int arm_cspmu_impl_register(const struct arm_cspmu_impl_match *impl_match)
1456 {
1457 	struct arm_cspmu_impl_match *match;
1458 	int ret = 0;
1459 
1460 	match = arm_cspmu_impl_match_get(impl_match->pmiidr_val);
1461 
1462 	if (match) {
1463 		mutex_lock(&arm_cspmu_lock);
1464 
1465 		if (!match->impl_init_ops) {
1466 			match->module = impl_match->module;
1467 			match->impl_init_ops = impl_match->impl_init_ops;
1468 		} else {
1469 			/* Broken match table may contain non-unique entries */
1470 			WARN(1, "arm_cspmu backend already registered for module: %s, pmiidr: 0x%x, mask: 0x%x\n",
1471 				match->module_name,
1472 				match->pmiidr_val,
1473 				match->pmiidr_mask);
1474 
1475 			ret = -EINVAL;
1476 		}
1477 
1478 		mutex_unlock(&arm_cspmu_lock);
1479 
1480 		if (!ret)
1481 			ret = driver_attach(&arm_cspmu_driver.driver);
1482 	} else {
1483 		pr_err("arm_cspmu reg failed, unable to find a match for pmiidr: 0x%x\n",
1484 			impl_match->pmiidr_val);
1485 
1486 		ret = -EINVAL;
1487 	}
1488 
1489 	return ret;
1490 }
1491 EXPORT_SYMBOL_GPL(arm_cspmu_impl_register);
1492 
arm_cspmu_match_device(struct device * dev,const void * match)1493 static int arm_cspmu_match_device(struct device *dev, const void *match)
1494 {
1495 	struct arm_cspmu *cspmu = platform_get_drvdata(to_platform_device(dev));
1496 
1497 	return (cspmu && cspmu->impl.match == match) ? 1 : 0;
1498 }
1499 
arm_cspmu_impl_unregister(const struct arm_cspmu_impl_match * impl_match)1500 void arm_cspmu_impl_unregister(const struct arm_cspmu_impl_match *impl_match)
1501 {
1502 	struct device *dev;
1503 	struct arm_cspmu_impl_match *match;
1504 
1505 	match = arm_cspmu_impl_match_get(impl_match->pmiidr_val);
1506 
1507 	if (WARN_ON(!match))
1508 		return;
1509 
1510 	/* Unbind the driver from all matching backend devices. */
1511 	while ((dev = driver_find_device(&arm_cspmu_driver.driver, NULL,
1512 			match, arm_cspmu_match_device))) {
1513 		device_release_driver(dev);
1514 		put_device(dev);
1515 	}
1516 
1517 	mutex_lock(&arm_cspmu_lock);
1518 
1519 	match->module = NULL;
1520 	match->impl_init_ops = NULL;
1521 
1522 	mutex_unlock(&arm_cspmu_lock);
1523 }
1524 EXPORT_SYMBOL_GPL(arm_cspmu_impl_unregister);
1525 
1526 module_init(arm_cspmu_init);
1527 module_exit(arm_cspmu_exit);
1528 
1529 MODULE_DESCRIPTION("ARM CoreSight Architecture Performance Monitor Driver");
1530 MODULE_LICENSE("GPL v2");
1531