driver inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/9163 ---------------------------------------------------------------------- The HiSilicon uncore frequency scaling driver is going to estimate the uncore load by counting L3C PMU events with in-kernel perf counters. perf_event_create_kernel_counter() requires perf_event_attr::type, but the type of a dynamic PMU is only known to its driver after perf_pmu_register(). Similar to how arm_pmu exposes its PMUs to KVM via kvm_host_pmu_init(), keep a list of HiSilicon uncore PMUs in the common hisi_uncore_pmu module and provide hisi_uncore_pmu_get_type() for other drivers to find the perf type of a PMU by its backing device. Only the L3C PMU is added to the list for now as it is the only user. Other HiSilicon uncore PMUs can opt in by calling hisi_uncore_pmu_list_add() and hisi_uncore_pmu_list_del(). Signed-off-by: Jie Zhan <zhanjie9@hisilicon.com> --- MAINTAINERS | 1 + drivers/perf/hisilicon/hisi_uncore_l3c_pmu.c | 6 ++- drivers/perf/hisilicon/hisi_uncore_pmu.c | 57 ++++++++++++++++++++ drivers/perf/hisilicon/hisi_uncore_pmu.h | 5 ++ include/linux/perf/hisi_pmu.h | 24 +++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 include/linux/perf/hisi_pmu.h diff --git a/MAINTAINERS b/MAINTAINERS index 5e44ed384a38..3d2cb642a54e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -9528,6 +9528,7 @@ W: http://www.hisilicon.com F: Documentation/admin-guide/perf/hisi-pcie-pmu.rst F: Documentation/admin-guide/perf/hisi-pmu.rst F: drivers/perf/hisilicon +F: include/linux/perf/hisi_pmu.h HISILICON PTT DRIVER M: Yicong Yang <yangyicong@hisilicon.com> diff --git a/drivers/perf/hisilicon/hisi_uncore_l3c_pmu.c b/drivers/perf/hisilicon/hisi_uncore_l3c_pmu.c index 805db48ad95f..e330f2b4f4bf 100644 --- a/drivers/perf/hisilicon/hisi_uncore_l3c_pmu.c +++ b/drivers/perf/hisilicon/hisi_uncore_l3c_pmu.c @@ -952,15 +952,19 @@ static int hisi_l3c_pmu_probe(struct platform_device *pdev) dev_err(l3c_pmu->dev, "L3C PMU register failed!\n"); cpuhp_state_remove_instance_nocalls( CPUHP_AP_PERF_ARM_HISI_L3_ONLINE, &l3c_pmu->node); + return ret; } - return ret; + hisi_uncore_pmu_list_add(l3c_pmu); + + return 0; } static int hisi_l3c_pmu_remove(struct platform_device *pdev) { struct hisi_pmu *l3c_pmu = platform_get_drvdata(pdev); + hisi_uncore_pmu_list_del(l3c_pmu); perf_pmu_unregister(&l3c_pmu->pmu); cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_HISI_L3_ONLINE, &l3c_pmu->node); diff --git a/drivers/perf/hisilicon/hisi_uncore_pmu.c b/drivers/perf/hisilicon/hisi_uncore_pmu.c index 786e162f2a89..aed1ad4d5fe3 100644 --- a/drivers/perf/hisilicon/hisi_uncore_pmu.c +++ b/drivers/perf/hisilicon/hisi_uncore_pmu.c @@ -11,9 +11,12 @@ #include <linux/bitmap.h> #include <linux/bitops.h> #include <linux/bug.h> +#include <linux/cleanup.h> #include <linux/err.h> #include <linux/errno.h> #include <linux/interrupt.h> +#include <linux/mutex.h> +#include <linux/perf/hisi_pmu.h> #include <linux/property.h> #include <asm/cputype.h> @@ -23,6 +26,10 @@ #define HISI_MAX_PERIOD(nr) (GENMASK_ULL((nr) - 1, 0)) +/* Registered PMUs that in-kernel users can look up */ +static LIST_HEAD(hisi_pmu_list); +static DEFINE_MUTEX(hisi_pmu_list_lock); + /* * PMU format attributes */ @@ -639,4 +646,54 @@ void hisi_pmu_init(struct hisi_pmu *hisi_pmu, struct module *module) } EXPORT_SYMBOL_NS_GPL(hisi_pmu_init, HISI_PMU); +/** + * hisi_uncore_pmu_list_add - Expose a PMU to in-kernel users + * @hisi_pmu: the PMU, already registered with perf_pmu_register() + */ +void hisi_uncore_pmu_list_add(struct hisi_pmu *hisi_pmu) +{ + guard(mutex)(&hisi_pmu_list_lock); + list_add_tail(&hisi_pmu->entry, &hisi_pmu_list); +} +EXPORT_SYMBOL_NS_GPL(hisi_uncore_pmu_list_add, HISI_PMU); + +/** + * hisi_uncore_pmu_list_del - Hide a PMU from in-kernel users + * @hisi_pmu: the PMU, to be unregistered with perf_pmu_unregister() + */ +void hisi_uncore_pmu_list_del(struct hisi_pmu *hisi_pmu) +{ + guard(mutex)(&hisi_pmu_list_lock); + list_del(&hisi_pmu->entry); +} +EXPORT_SYMBOL_NS_GPL(hisi_uncore_pmu_list_del, HISI_PMU); + +/** + * hisi_uncore_pmu_get_type - Get the perf type of a HiSilicon uncore PMU + * @dev: the device backing the PMU, e.g. the L3C PMU platform device + * + * Allow in-kernel users to fill perf_event_attr::type for + * perf_event_create_kernel_counter(). Only PMUs whose driver has called + * hisi_uncore_pmu_list_add() can be found. + * + * The PMU may go away right after the lookup, in which case creating a + * counter with the returned type fails. Once a counter has been created, the + * PMU module is pinned until the counter is released. + * + * Return: the perf type of the PMU, or -ENODEV if no such PMU is registered. + */ +int hisi_uncore_pmu_get_type(struct device *dev) +{ + struct hisi_pmu *hisi_pmu; + + guard(mutex)(&hisi_pmu_list_lock); + list_for_each_entry(hisi_pmu, &hisi_pmu_list, entry) { + if (hisi_pmu->dev == dev) + return hisi_pmu->pmu.type; + } + + return -ENODEV; +} +EXPORT_SYMBOL_NS_GPL(hisi_uncore_pmu_get_type, HISI_PMU); + MODULE_LICENSE("GPL v2"); diff --git a/drivers/perf/hisilicon/hisi_uncore_pmu.h b/drivers/perf/hisilicon/hisi_uncore_pmu.h index bdf17d1a3099..d0d67f642109 100644 --- a/drivers/perf/hisilicon/hisi_uncore_pmu.h +++ b/drivers/perf/hisilicon/hisi_uncore_pmu.h @@ -15,6 +15,7 @@ #include <linux/cpumask.h> #include <linux/device.h> #include <linux/kernel.h> +#include <linux/list.h> #include <linux/module.h> #include <linux/perf_event.h> #include <linux/platform_device.h> @@ -132,6 +133,8 @@ struct hisi_pmu { int irq; struct device *dev; struct hlist_node node; + /* Entry in the list of PMUs exposed to in-kernel users */ + struct list_head entry; void __iomem *base; int num_counters; int counter_bits; @@ -173,4 +176,6 @@ int hisi_uncore_pmu_init_irq(struct hisi_pmu *hisi_pmu, void hisi_uncore_pmu_init_topology(struct hisi_pmu *hisi_pmu, struct device *dev); void hisi_pmu_init(struct hisi_pmu *hisi_pmu, struct module *module); +void hisi_uncore_pmu_list_add(struct hisi_pmu *hisi_pmu); +void hisi_uncore_pmu_list_del(struct hisi_pmu *hisi_pmu); #endif /* __HISI_UNCORE_PMU_H__ */ diff --git a/include/linux/perf/hisi_pmu.h b/include/linux/perf/hisi_pmu.h new file mode 100644 index 000000000000..7d71c7a0e8de --- /dev/null +++ b/include/linux/perf/hisi_pmu.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * HiSilicon SoC uncore PMU interface for in-kernel users + * + * Copyright (c) 2026 HiSilicon Limited. + */ + +#ifndef __LINUX_PERF_HISI_PMU_H +#define __LINUX_PERF_HISI_PMU_H + +#include <linux/errno.h> + +struct device; + +#if IS_ENABLED(CONFIG_HISI_PMU) +int hisi_uncore_pmu_get_type(struct device *dev); +#else +static inline int hisi_uncore_pmu_get_type(struct device *dev) +{ + return -ENODEV; +} +#endif + +#endif /* __LINUX_PERF_HISI_PMU_H */ -- 2.43.0