xref: /linux/drivers/perf/hisilicon/hisi_uncore_pmu.h (revision f96a974170b749e3a56844e25b31d46a7233b6f6)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * HiSilicon SoC Hardware event counters support
4  *
5  * Copyright (C) 2017 HiSilicon Limited
6  * Author: Anurup M <anurup.m@huawei.com>
7  *         Shaokun Zhang <zhangshaokun@hisilicon.com>
8  *
9  * This code is based on the uncore PMUs like arm-cci and arm-ccn.
10  */
11 #ifndef __HISI_UNCORE_PMU_H__
12 #define __HISI_UNCORE_PMU_H__
13 
14 #include <linux/bitfield.h>
15 #include <linux/cpumask.h>
16 #include <linux/device.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/perf_event.h>
20 #include <linux/platform_device.h>
21 #include <linux/types.h>
22 
23 #undef pr_fmt
24 #define pr_fmt(fmt)     "hisi_pmu: " fmt
25 
26 #define HISI_PMU_V2		0x30
27 #define HISI_MAX_COUNTERS 0x10
28 #define to_hisi_pmu(p)	(container_of(p, struct hisi_pmu, pmu))
29 
30 #define HISI_PMU_ATTR(_name, _func, _config)				\
31 	(&((struct dev_ext_attribute[]) {				\
32 		{ __ATTR(_name, 0444, _func, NULL), (void *)_config }   \
33 	})[0].attr.attr)
34 
35 #define HISI_PMU_FORMAT_ATTR(_name, _config)		\
36 	HISI_PMU_ATTR(_name, device_show_string, _config)
37 #define HISI_PMU_EVENT_ATTR(_name, _config)		\
38 	HISI_PMU_ATTR(_name, hisi_event_sysfs_show, (unsigned long)_config)
39 
40 #define HISI_PMU_EVENT_ATTR_EXTRACTOR(name, config, hi, lo)        \
41 	static inline u32 hisi_get_##name(struct perf_event *event)            \
42 	{                                                                  \
43 		return FIELD_GET(GENMASK_ULL(hi, lo), event->attr.config);  \
44 	}
45 
46 #define HISI_GET_EVENTID(ev) (ev->hw.config_base & 0xff)
47 
48 #define HISI_PMU_EVTYPE_BITS		8
49 #define HISI_PMU_EVTYPE_SHIFT(idx)	((idx) % 4 * HISI_PMU_EVTYPE_BITS)
50 
51 struct hisi_pmu;
52 
53 struct hisi_uncore_ops {
54 	int (*check_filter)(struct perf_event *event);
55 	void (*write_evtype)(struct hisi_pmu *, int, u32);
56 	int (*get_event_idx)(struct perf_event *);
57 	u64 (*read_counter)(struct hisi_pmu *, struct hw_perf_event *);
58 	void (*write_counter)(struct hisi_pmu *, struct hw_perf_event *, u64);
59 	void (*enable_counter)(struct hisi_pmu *, struct hw_perf_event *);
60 	void (*disable_counter)(struct hisi_pmu *, struct hw_perf_event *);
61 	void (*enable_counter_int)(struct hisi_pmu *, struct hw_perf_event *);
62 	void (*disable_counter_int)(struct hisi_pmu *, struct hw_perf_event *);
63 	void (*start_counters)(struct hisi_pmu *);
64 	void (*stop_counters)(struct hisi_pmu *);
65 	u32 (*get_int_status)(struct hisi_pmu *hisi_pmu);
66 	void (*clear_int_status)(struct hisi_pmu *hisi_pmu, int idx);
67 	void (*enable_filter)(struct perf_event *event);
68 	void (*disable_filter)(struct perf_event *event);
69 };
70 
71 /* Describes the HISI PMU chip features information */
72 struct hisi_pmu_dev_info {
73 	const char *name;
74 	const struct attribute_group **attr_groups;
75 	void *private;
76 };
77 
78 struct hisi_pmu_hwevents {
79 	struct perf_event *hw_events[HISI_MAX_COUNTERS];
80 	DECLARE_BITMAP(used_mask, HISI_MAX_COUNTERS);
81 	const struct attribute_group **attr_groups;
82 };
83 
84 /**
85  * struct hisi_pmu_topology - Describe the topology hierarchy on which the PMU
86  *                            is located.
87  * @sccl_id: ID of the SCCL on which the PMU locate is located.
88  * @sicl_id: ID of the SICL on which the PMU locate is located.
89  * @scl_id:  ID used by the core which is unaware of the SCCL/SICL.
90  * @ccl_id: ID of the CCL (CPU cluster) on which the PMU is located.
91  * @index_id: the ID of the PMU module if there're several PMUs at a
92  *            particularly location in the topology.
93  * @sub_id: submodule ID of the PMU. For example we use this for DDRC PMU v2
94  *          since each DDRC has more than one DMC
95  *
96  * The ID will be -1 if the PMU isn't located on a certain topology.
97  */
98 struct hisi_pmu_topology {
99 	/*
100 	 * SCCL (Super CPU CLuster) and SICL (Super I/O Cluster) are parallel
101 	 * so a PMU cannot locate on a SCCL and a SICL. If the SCCL/SICL
102 	 * distinction is not relevant, use scl_id instead.
103 	 */
104 	union {
105 		int sccl_id;
106 		int sicl_id;
107 		int scl_id;
108 	};
109 	int ccl_id;
110 	int index_id;
111 	int sub_id;
112 };
113 
114 /* Generic pmu struct for different pmu types */
115 struct hisi_pmu {
116 	struct pmu pmu;
117 	const struct hisi_uncore_ops *ops;
118 	const struct hisi_pmu_dev_info *dev_info;
119 	struct hisi_pmu_hwevents pmu_events;
120 	struct hisi_pmu_topology topo;
121 	/*
122 	 * CPUs associated to the PMU and are preferred to use for counting.
123 	 * Could be empty if PMU has no association (e.g. PMU on SICL), in
124 	 * which case any online CPU will be used.
125 	 */
126 	cpumask_t associated_cpus;
127 	/* CPU used for counting */
128 	int on_cpu;
129 	int irq;
130 	struct device *dev;
131 	struct hlist_node node;
132 	void __iomem *base;
133 	int num_counters;
134 	int counter_bits;
135 	/* check event code range */
136 	int check_event;
137 	u32 identifier;
138 };
139 
140 /* Generic implementation of cpumask/identifier group */
141 extern const struct attribute_group hisi_pmu_cpumask_attr_group;
142 extern const struct attribute_group hisi_pmu_identifier_group;
143 
144 int hisi_uncore_pmu_get_event_idx(struct perf_event *event);
145 void hisi_uncore_pmu_read(struct perf_event *event);
146 int hisi_uncore_pmu_add(struct perf_event *event, int flags);
147 void hisi_uncore_pmu_del(struct perf_event *event, int flags);
148 void hisi_uncore_pmu_start(struct perf_event *event, int flags);
149 void hisi_uncore_pmu_stop(struct perf_event *event, int flags);
150 void hisi_uncore_pmu_set_event_period(struct perf_event *event);
151 void hisi_uncore_pmu_event_update(struct perf_event *event);
152 int hisi_uncore_pmu_event_init(struct perf_event *event);
153 void hisi_uncore_pmu_enable(struct pmu *pmu);
154 void hisi_uncore_pmu_disable(struct pmu *pmu);
155 ssize_t hisi_event_sysfs_show(struct device *dev,
156 			      struct device_attribute *attr, char *buf);
157 ssize_t hisi_cpumask_sysfs_show(struct device *dev,
158 				struct device_attribute *attr, char *buf);
159 int hisi_uncore_pmu_online_cpu(unsigned int cpu, struct hlist_node *node);
160 int hisi_uncore_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node);
161 
162 ssize_t hisi_uncore_pmu_identifier_attr_show(struct device *dev,
163 					     struct device_attribute *attr,
164 					     char *page);
165 int hisi_uncore_pmu_init_irq(struct hisi_pmu *hisi_pmu,
166 			     struct platform_device *pdev);
167 void hisi_uncore_pmu_init_topology(struct hisi_pmu *hisi_pmu, struct device *dev);
168 
169 void hisi_pmu_init(struct hisi_pmu *hisi_pmu, struct module *module);
170 #endif /* __HISI_UNCORE_PMU_H__ */
171