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 u32 counter_bits; 76 u32 check_event; 77 void *private; 78 }; 79 80 struct hisi_pmu_hwevents { 81 struct perf_event *hw_events[HISI_MAX_COUNTERS]; 82 DECLARE_BITMAP(used_mask, HISI_MAX_COUNTERS); 83 const struct attribute_group **attr_groups; 84 }; 85 86 /** 87 * struct hisi_pmu_topology - Describe the topology hierarchy on which the PMU 88 * is located. 89 * @sccl_id: ID of the SCCL on which the PMU locate is located. 90 * @sicl_id: ID of the SICL on which the PMU locate is located. 91 * @scl_id: ID used by the core which is unaware of the SCCL/SICL. 92 * @ccl_id: ID of the CCL (CPU cluster) on which the PMU is located. 93 * @index_id: the ID of the PMU module if there're several PMUs at a 94 * particularly location in the topology. 95 * @sub_id: submodule ID of the PMU. For example we use this for DDRC PMU v2 96 * since each DDRC has more than one DMC 97 * 98 * The ID will be -1 if the PMU isn't located on a certain topology. 99 */ 100 struct hisi_pmu_topology { 101 /* 102 * SCCL (Super CPU CLuster) and SICL (Super I/O Cluster) are parallel 103 * so a PMU cannot locate on a SCCL and a SICL. If the SCCL/SICL 104 * distinction is not relevant, use scl_id instead. 105 */ 106 union { 107 int sccl_id; 108 int sicl_id; 109 int scl_id; 110 }; 111 int ccl_id; 112 int index_id; 113 int sub_id; 114 }; 115 116 /* Generic pmu struct for different pmu types */ 117 struct hisi_pmu { 118 struct pmu pmu; 119 const struct hisi_uncore_ops *ops; 120 const struct hisi_pmu_dev_info *dev_info; 121 struct hisi_pmu_hwevents pmu_events; 122 struct hisi_pmu_topology topo; 123 /* 124 * CPUs associated to the PMU and are preferred to use for counting. 125 * Could be empty if PMU has no association (e.g. PMU on SICL), in 126 * which case any online CPU will be used. 127 */ 128 cpumask_t associated_cpus; 129 /* CPU used for counting */ 130 int on_cpu; 131 int irq; 132 struct device *dev; 133 struct hlist_node node; 134 void __iomem *base; 135 int num_counters; 136 int counter_bits; 137 /* check event code range */ 138 int check_event; 139 u32 identifier; 140 }; 141 142 /* Generic implementation of cpumask/identifier group */ 143 extern const struct attribute_group hisi_pmu_cpumask_attr_group; 144 extern const struct attribute_group hisi_pmu_identifier_group; 145 146 int hisi_uncore_pmu_get_event_idx(struct perf_event *event); 147 void hisi_uncore_pmu_read(struct perf_event *event); 148 int hisi_uncore_pmu_add(struct perf_event *event, int flags); 149 void hisi_uncore_pmu_del(struct perf_event *event, int flags); 150 void hisi_uncore_pmu_start(struct perf_event *event, int flags); 151 void hisi_uncore_pmu_stop(struct perf_event *event, int flags); 152 void hisi_uncore_pmu_set_event_period(struct perf_event *event); 153 void hisi_uncore_pmu_event_update(struct perf_event *event); 154 int hisi_uncore_pmu_event_init(struct perf_event *event); 155 void hisi_uncore_pmu_enable(struct pmu *pmu); 156 void hisi_uncore_pmu_disable(struct pmu *pmu); 157 ssize_t hisi_event_sysfs_show(struct device *dev, 158 struct device_attribute *attr, char *buf); 159 ssize_t hisi_cpumask_sysfs_show(struct device *dev, 160 struct device_attribute *attr, char *buf); 161 int hisi_uncore_pmu_online_cpu(unsigned int cpu, struct hlist_node *node); 162 int hisi_uncore_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node); 163 164 ssize_t hisi_uncore_pmu_identifier_attr_show(struct device *dev, 165 struct device_attribute *attr, 166 char *page); 167 int hisi_uncore_pmu_init_irq(struct hisi_pmu *hisi_pmu, 168 struct platform_device *pdev); 169 void hisi_uncore_pmu_init_topology(struct hisi_pmu *hisi_pmu, struct device *dev); 170 171 void hisi_pmu_init(struct hisi_pmu *hisi_pmu, struct module *module); 172 #endif /* __HISI_UNCORE_PMU_H__ */ 173