1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
26bcb2db5SBorislav Petkov #include <linux/slab.h>
36bcb2db5SBorislav Petkov #include <linux/pci.h>
46f6e1516SIngo Molnar #include <asm/apicdef.h>
5efe06270SAlexander Antonov #include <asm/intel-family.h>
607ce734dSKan Liang #include <linux/io-64-nonatomic-lo-hi.h>
76f6e1516SIngo Molnar
86bcb2db5SBorislav Petkov #include <linux/perf_event.h>
927f6d22bSBorislav Petkov #include "../perf_event.h"
106bcb2db5SBorislav Petkov
116bcb2db5SBorislav Petkov #define UNCORE_PMU_NAME_LEN 32
126bcb2db5SBorislav Petkov #define UNCORE_PMU_HRTIMER_INTERVAL (60LL * NSEC_PER_SEC)
136bcb2db5SBorislav Petkov #define UNCORE_SNB_IMC_HRTIMER_INTERVAL (5ULL * NSEC_PER_SEC)
146bcb2db5SBorislav Petkov
156bcb2db5SBorislav Petkov #define UNCORE_FIXED_EVENT 0xff
166bcb2db5SBorislav Petkov #define UNCORE_PMC_IDX_MAX_GENERIC 8
17927b2debSKan Liang #define UNCORE_PMC_IDX_MAX_FIXED 1
18927b2debSKan Liang #define UNCORE_PMC_IDX_MAX_FREERUNNING 1
196bcb2db5SBorislav Petkov #define UNCORE_PMC_IDX_FIXED UNCORE_PMC_IDX_MAX_GENERIC
20927b2debSKan Liang #define UNCORE_PMC_IDX_FREERUNNING (UNCORE_PMC_IDX_FIXED + \
21927b2debSKan Liang UNCORE_PMC_IDX_MAX_FIXED)
22927b2debSKan Liang #define UNCORE_PMC_IDX_MAX (UNCORE_PMC_IDX_FREERUNNING + \
23927b2debSKan Liang UNCORE_PMC_IDX_MAX_FREERUNNING)
246bcb2db5SBorislav Petkov
25a54fa079SKan Liang #define UNCORE_PCI_DEV_FULL_DATA(dev, func, type, idx) \
26a54fa079SKan Liang ((dev << 24) | (func << 16) | (type << 8) | idx)
276bcb2db5SBorislav Petkov #define UNCORE_PCI_DEV_DATA(type, idx) ((type << 8) | idx)
28a54fa079SKan Liang #define UNCORE_PCI_DEV_DEV(data) ((data >> 24) & 0xff)
29a54fa079SKan Liang #define UNCORE_PCI_DEV_FUNC(data) ((data >> 16) & 0xff)
306bcb2db5SBorislav Petkov #define UNCORE_PCI_DEV_TYPE(data) ((data >> 8) & 0xff)
316bcb2db5SBorislav Petkov #define UNCORE_PCI_DEV_IDX(data) (data & 0xff)
326bcb2db5SBorislav Petkov #define UNCORE_EXTRA_PCI_DEV 0xff
33156c8b58SKan Liang #define UNCORE_EXTRA_PCI_DEV_MAX 4
346bcb2db5SBorislav Petkov
356bcb2db5SBorislav Petkov #define UNCORE_EVENT_CONSTRAINT(c, n) EVENT_CONSTRAINT(c, n, 0xff)
366bcb2db5SBorislav Petkov
37bd9514a4SKan Liang #define UNCORE_IGNORE_END -1
38bd9514a4SKan Liang
39cf6d445fSThomas Gleixner struct pci_extra_dev {
40cf6d445fSThomas Gleixner struct pci_dev *dev[UNCORE_EXTRA_PCI_DEV_MAX];
41cf6d445fSThomas Gleixner };
42cf6d445fSThomas Gleixner
436bcb2db5SBorislav Petkov struct intel_uncore_ops;
446bcb2db5SBorislav Petkov struct intel_uncore_pmu;
456bcb2db5SBorislav Petkov struct intel_uncore_box;
466bcb2db5SBorislav Petkov struct uncore_event_desc;
47927b2debSKan Liang struct freerunning_counters;
48cface032SAlexander Antonov struct intel_uncore_topology;
496bcb2db5SBorislav Petkov
506bcb2db5SBorislav Petkov struct intel_uncore_type {
516bcb2db5SBorislav Petkov const char *name;
526bcb2db5SBorislav Petkov int num_counters;
536bcb2db5SBorislav Petkov int num_boxes;
546bcb2db5SBorislav Petkov int perf_ctr_bits;
556bcb2db5SBorislav Petkov int fixed_ctr_bits;
56927b2debSKan Liang int num_freerunning_types;
57d6c75413SKan Liang int type_id;
586bcb2db5SBorislav Petkov unsigned perf_ctr;
596bcb2db5SBorislav Petkov unsigned event_ctl;
606bcb2db5SBorislav Petkov unsigned event_mask;
61cd34cd97SKan Liang unsigned event_mask_ext;
626bcb2db5SBorislav Petkov unsigned fixed_ctr;
636bcb2db5SBorislav Petkov unsigned fixed_ctl;
646bcb2db5SBorislav Petkov unsigned box_ctl;
6542839ef4SKan Liang u64 *box_ctls; /* Unit ctrl addr of the first box of each die */
663da04b8aSKan Liang union {
676bcb2db5SBorislav Petkov unsigned msr_offset;
683da04b8aSKan Liang unsigned mmio_offset;
693da04b8aSKan Liang };
701b94d31dSKan Liang unsigned mmio_map_size;
716bcb2db5SBorislav Petkov unsigned num_shared_regs:8;
726bcb2db5SBorislav Petkov unsigned single_fixed:1;
736bcb2db5SBorislav Petkov unsigned pair_ctr_ctl:1;
7442839ef4SKan Liang union {
756bcb2db5SBorislav Petkov unsigned *msr_offsets;
7642839ef4SKan Liang unsigned *pci_offsets;
77c4c55e36SKan Liang unsigned *mmio_offsets;
7842839ef4SKan Liang };
79d6c75413SKan Liang unsigned *box_ids;
806bcb2db5SBorislav Petkov struct event_constraint unconstrainted;
816bcb2db5SBorislav Petkov struct event_constraint *constraints;
826bcb2db5SBorislav Petkov struct intel_uncore_pmu *pmus;
836bcb2db5SBorislav Petkov struct intel_uncore_ops *ops;
846bcb2db5SBorislav Petkov struct uncore_event_desc *event_descs;
85927b2debSKan Liang struct freerunning_counters *freerunning;
866bcb2db5SBorislav Petkov const struct attribute_group *attr_groups[4];
8719a39819SRoman Sudarikov const struct attribute_group **attr_update;
886bcb2db5SBorislav Petkov struct pmu *pmu; /* for custom pmu ops */
8919a39819SRoman Sudarikov /*
9019a39819SRoman Sudarikov * Uncore PMU would store relevant platform topology configuration here
9119a39819SRoman Sudarikov * to identify which platform component each PMON block of that type is
9219a39819SRoman Sudarikov * supposed to monitor.
9319a39819SRoman Sudarikov */
944d13be8aSAlexander Antonov struct intel_uncore_topology **topology;
9519a39819SRoman Sudarikov /*
9619a39819SRoman Sudarikov * Optional callbacks for managing mapping of Uncore units to PMONs
9719a39819SRoman Sudarikov */
98f471fac7SAlexander Antonov int (*get_topology)(struct intel_uncore_type *type);
99d5b73506SAlexander Antonov void (*set_mapping)(struct intel_uncore_type *type);
10019a39819SRoman Sudarikov void (*cleanup_mapping)(struct intel_uncore_type *type);
1016bcb2db5SBorislav Petkov };
1026bcb2db5SBorislav Petkov
1036bcb2db5SBorislav Petkov #define pmu_group attr_groups[0]
1046bcb2db5SBorislav Petkov #define format_group attr_groups[1]
1056bcb2db5SBorislav Petkov #define events_group attr_groups[2]
1066bcb2db5SBorislav Petkov
1076bcb2db5SBorislav Petkov struct intel_uncore_ops {
1086bcb2db5SBorislav Petkov void (*init_box)(struct intel_uncore_box *);
109a46195f1SThomas Gleixner void (*exit_box)(struct intel_uncore_box *);
1106bcb2db5SBorislav Petkov void (*disable_box)(struct intel_uncore_box *);
1116bcb2db5SBorislav Petkov void (*enable_box)(struct intel_uncore_box *);
1126bcb2db5SBorislav Petkov void (*disable_event)(struct intel_uncore_box *, struct perf_event *);
1136bcb2db5SBorislav Petkov void (*enable_event)(struct intel_uncore_box *, struct perf_event *);
1146bcb2db5SBorislav Petkov u64 (*read_counter)(struct intel_uncore_box *, struct perf_event *);
1156bcb2db5SBorislav Petkov int (*hw_config)(struct intel_uncore_box *, struct perf_event *);
1166bcb2db5SBorislav Petkov struct event_constraint *(*get_constraint)(struct intel_uncore_box *,
1176bcb2db5SBorislav Petkov struct perf_event *);
1186bcb2db5SBorislav Petkov void (*put_constraint)(struct intel_uncore_box *, struct perf_event *);
1196bcb2db5SBorislav Petkov };
1206bcb2db5SBorislav Petkov
1216bcb2db5SBorislav Petkov struct intel_uncore_pmu {
1226bcb2db5SBorislav Petkov struct pmu pmu;
1236bcb2db5SBorislav Petkov char name[UNCORE_PMU_NAME_LEN];
1246bcb2db5SBorislav Petkov int pmu_idx;
1256bcb2db5SBorislav Petkov int func_id;
1264f089678SThomas Gleixner bool registered;
127cf6d445fSThomas Gleixner atomic_t activeboxes;
1286bcb2db5SBorislav Petkov struct intel_uncore_type *type;
129cf6d445fSThomas Gleixner struct intel_uncore_box **boxes;
1306bcb2db5SBorislav Petkov };
1316bcb2db5SBorislav Petkov
1326bcb2db5SBorislav Petkov struct intel_uncore_extra_reg {
1336bcb2db5SBorislav Petkov raw_spinlock_t lock;
1346bcb2db5SBorislav Petkov u64 config, config1, config2;
1356bcb2db5SBorislav Petkov atomic_t ref;
1366bcb2db5SBorislav Petkov };
1376bcb2db5SBorislav Petkov
1386bcb2db5SBorislav Petkov struct intel_uncore_box {
139b0529b9cSKan Liang int dieid; /* Logical die ID */
1406bcb2db5SBorislav Petkov int n_active; /* number of active events */
1416bcb2db5SBorislav Petkov int n_events;
1426bcb2db5SBorislav Petkov int cpu; /* cpu to collect events */
1436bcb2db5SBorislav Petkov unsigned long flags;
1446bcb2db5SBorislav Petkov atomic_t refcnt;
1456bcb2db5SBorislav Petkov struct perf_event *events[UNCORE_PMC_IDX_MAX];
1466bcb2db5SBorislav Petkov struct perf_event *event_list[UNCORE_PMC_IDX_MAX];
1476bcb2db5SBorislav Petkov struct event_constraint *event_constraint[UNCORE_PMC_IDX_MAX];
1486bcb2db5SBorislav Petkov unsigned long active_mask[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX)];
1496bcb2db5SBorislav Petkov u64 tags[UNCORE_PMC_IDX_MAX];
1506bcb2db5SBorislav Petkov struct pci_dev *pci_dev;
1516bcb2db5SBorislav Petkov struct intel_uncore_pmu *pmu;
1526bcb2db5SBorislav Petkov u64 hrtimer_duration; /* hrtimer timeout for this box */
1536bcb2db5SBorislav Petkov struct hrtimer hrtimer;
1546bcb2db5SBorislav Petkov struct list_head list;
1556bcb2db5SBorislav Petkov struct list_head active_list;
15607ce734dSKan Liang void __iomem *io_addr;
1578ac7571aSGustavo A. R. Silva struct intel_uncore_extra_reg shared_regs[];
1586bcb2db5SBorislav Petkov };
1596bcb2db5SBorislav Petkov
1604d47d640SKan Liang /* CFL uncore 8th cbox MSRs */
1614d47d640SKan Liang #define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70
1624d47d640SKan Liang #define CFL_UNC_CBO_7_PER_CTR0 0xf76
1634d47d640SKan Liang
1646bcb2db5SBorislav Petkov #define UNCORE_BOX_FLAG_INITIATED 0
1654d47d640SKan Liang /* event config registers are 8-byte apart */
1664d47d640SKan Liang #define UNCORE_BOX_FLAG_CTL_OFFS8 1
1674d47d640SKan Liang /* CFL 8th CBOX has different MSR space */
1684d47d640SKan Liang #define UNCORE_BOX_FLAG_CFL8_CBOX_MSR_OFFS 2
1696bcb2db5SBorislav Petkov
1706bcb2db5SBorislav Petkov struct uncore_event_desc {
171ebd19fc3SSami Tolvanen struct device_attribute attr;
1726bcb2db5SBorislav Petkov const char *config;
1736bcb2db5SBorislav Petkov };
1746bcb2db5SBorislav Petkov
175927b2debSKan Liang struct freerunning_counters {
176927b2debSKan Liang unsigned int counter_base;
177927b2debSKan Liang unsigned int counter_offset;
178927b2debSKan Liang unsigned int box_offset;
179927b2debSKan Liang unsigned int num_counters;
180927b2debSKan Liang unsigned int bits;
181bc88a2feSKan Liang unsigned *box_offsets;
182927b2debSKan Liang };
183927b2debSKan Liang
1844d13be8aSAlexander Antonov struct uncore_iio_topology {
1854d13be8aSAlexander Antonov int pci_bus_no;
186cface032SAlexander Antonov int segment;
187cface032SAlexander Antonov };
188cface032SAlexander Antonov
189cee4eebdSAlexander Antonov struct uncore_upi_topology {
190cee4eebdSAlexander Antonov int die_to;
191cee4eebdSAlexander Antonov int pmu_idx_to;
192cee4eebdSAlexander Antonov int enabled;
193cee4eebdSAlexander Antonov };
194cee4eebdSAlexander Antonov
1954d13be8aSAlexander Antonov struct intel_uncore_topology {
1964d13be8aSAlexander Antonov int pmu_idx;
1974d13be8aSAlexander Antonov union {
1984d13be8aSAlexander Antonov void *untyped;
1994d13be8aSAlexander Antonov struct uncore_iio_topology *iio;
200cee4eebdSAlexander Antonov struct uncore_upi_topology *upi;
2014d13be8aSAlexander Antonov };
2024d13be8aSAlexander Antonov };
2034d13be8aSAlexander Antonov
2046bcb2db5SBorislav Petkov struct pci2phy_map {
2056bcb2db5SBorislav Petkov struct list_head list;
2066bcb2db5SBorislav Petkov int segment;
207ba9506beSSteve Wahl int pbus_to_dieid[256];
2086bcb2db5SBorislav Petkov };
2096bcb2db5SBorislav Petkov
2106bcb2db5SBorislav Petkov struct pci2phy_map *__find_pci2phy_map(int segment);
211ba9506beSSteve Wahl int uncore_pcibus_to_dieid(struct pci_bus *bus);
212cface032SAlexander Antonov int uncore_die_to_segment(int die);
213dbf061b2SKan Liang int uncore_device_to_die(struct pci_dev *dev);
2146bcb2db5SBorislav Petkov
215ebd19fc3SSami Tolvanen ssize_t uncore_event_show(struct device *dev,
216ebd19fc3SSami Tolvanen struct device_attribute *attr, char *buf);
2176bcb2db5SBorislav Petkov
dev_to_uncore_pmu(struct device * dev)218bb42b3d3SRoman Sudarikov static inline struct intel_uncore_pmu *dev_to_uncore_pmu(struct device *dev)
219bb42b3d3SRoman Sudarikov {
220bb42b3d3SRoman Sudarikov return container_of(dev_get_drvdata(dev), struct intel_uncore_pmu, pmu);
221bb42b3d3SRoman Sudarikov }
222bb42b3d3SRoman Sudarikov
223bb42b3d3SRoman Sudarikov #define to_device_attribute(n) container_of(n, struct device_attribute, attr)
224bb42b3d3SRoman Sudarikov #define to_dev_ext_attribute(n) container_of(n, struct dev_ext_attribute, attr)
225bb42b3d3SRoman Sudarikov #define attr_to_ext_attr(n) to_dev_ext_attribute(to_device_attribute(n))
226bb42b3d3SRoman Sudarikov
22736b533bcSRoman Sudarikov extern int __uncore_max_dies;
22836b533bcSRoman Sudarikov #define uncore_max_dies() (__uncore_max_dies)
22936b533bcSRoman Sudarikov
2306bcb2db5SBorislav Petkov #define INTEL_UNCORE_EVENT_DESC(_name, _config) \
2316bcb2db5SBorislav Petkov { \
2326bcb2db5SBorislav Petkov .attr = __ATTR(_name, 0444, uncore_event_show, NULL), \
2336bcb2db5SBorislav Petkov .config = _config, \
2346bcb2db5SBorislav Petkov }
2356bcb2db5SBorislav Petkov
2366bcb2db5SBorislav Petkov #define DEFINE_UNCORE_FORMAT_ATTR(_var, _name, _format) \
237ebd19fc3SSami Tolvanen static ssize_t __uncore_##_var##_show(struct device *dev, \
238ebd19fc3SSami Tolvanen struct device_attribute *attr, \
2396bcb2db5SBorislav Petkov char *page) \
2406bcb2db5SBorislav Petkov { \
2416bcb2db5SBorislav Petkov BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
2426bcb2db5SBorislav Petkov return sprintf(page, _format "\n"); \
2436bcb2db5SBorislav Petkov } \
244ebd19fc3SSami Tolvanen static struct device_attribute format_attr_##_var = \
2456bcb2db5SBorislav Petkov __ATTR(_name, 0444, __uncore_##_var##_show, NULL)
2466bcb2db5SBorislav Petkov
uncore_pmc_fixed(int idx)247927b2debSKan Liang static inline bool uncore_pmc_fixed(int idx)
248927b2debSKan Liang {
249927b2debSKan Liang return idx == UNCORE_PMC_IDX_FIXED;
250927b2debSKan Liang }
251927b2debSKan Liang
uncore_pmc_freerunning(int idx)252927b2debSKan Liang static inline bool uncore_pmc_freerunning(int idx)
253927b2debSKan Liang {
254927b2debSKan Liang return idx == UNCORE_PMC_IDX_FREERUNNING;
255927b2debSKan Liang }
256927b2debSKan Liang
uncore_mmio_is_valid_offset(struct intel_uncore_box * box,unsigned long offset)257f0171973SKan Liang static inline bool uncore_mmio_is_valid_offset(struct intel_uncore_box *box,
258f0171973SKan Liang unsigned long offset)
259f0171973SKan Liang {
260f0171973SKan Liang if (offset < box->pmu->type->mmio_map_size)
261f0171973SKan Liang return true;
262f0171973SKan Liang
263f0171973SKan Liang pr_warn_once("perf uncore: Invalid offset 0x%lx exceeds mapped area of %s.\n",
264f0171973SKan Liang offset, box->pmu->type->name);
265f0171973SKan Liang
266f0171973SKan Liang return false;
267f0171973SKan Liang }
268f0171973SKan Liang
2693da04b8aSKan Liang static inline
uncore_mmio_box_ctl(struct intel_uncore_box * box)2703da04b8aSKan Liang unsigned int uncore_mmio_box_ctl(struct intel_uncore_box *box)
2713da04b8aSKan Liang {
2723da04b8aSKan Liang return box->pmu->type->box_ctl +
2733da04b8aSKan Liang box->pmu->type->mmio_offset * box->pmu->pmu_idx;
2743da04b8aSKan Liang }
2753da04b8aSKan Liang
uncore_pci_box_ctl(struct intel_uncore_box * box)2766bcb2db5SBorislav Petkov static inline unsigned uncore_pci_box_ctl(struct intel_uncore_box *box)
2776bcb2db5SBorislav Petkov {
2786bcb2db5SBorislav Petkov return box->pmu->type->box_ctl;
2796bcb2db5SBorislav Petkov }
2806bcb2db5SBorislav Petkov
uncore_pci_fixed_ctl(struct intel_uncore_box * box)2816bcb2db5SBorislav Petkov static inline unsigned uncore_pci_fixed_ctl(struct intel_uncore_box *box)
2826bcb2db5SBorislav Petkov {
2836bcb2db5SBorislav Petkov return box->pmu->type->fixed_ctl;
2846bcb2db5SBorislav Petkov }
2856bcb2db5SBorislav Petkov
uncore_pci_fixed_ctr(struct intel_uncore_box * box)2866bcb2db5SBorislav Petkov static inline unsigned uncore_pci_fixed_ctr(struct intel_uncore_box *box)
2876bcb2db5SBorislav Petkov {
2886bcb2db5SBorislav Petkov return box->pmu->type->fixed_ctr;
2896bcb2db5SBorislav Petkov }
2906bcb2db5SBorislav Petkov
2916bcb2db5SBorislav Petkov static inline
uncore_pci_event_ctl(struct intel_uncore_box * box,int idx)2926bcb2db5SBorislav Petkov unsigned uncore_pci_event_ctl(struct intel_uncore_box *box, int idx)
2936bcb2db5SBorislav Petkov {
29424cf8467SStephane Eranian if (test_bit(UNCORE_BOX_FLAG_CTL_OFFS8, &box->flags))
29524cf8467SStephane Eranian return idx * 8 + box->pmu->type->event_ctl;
29624cf8467SStephane Eranian
2976bcb2db5SBorislav Petkov return idx * 4 + box->pmu->type->event_ctl;
2986bcb2db5SBorislav Petkov }
2996bcb2db5SBorislav Petkov
3006bcb2db5SBorislav Petkov static inline
uncore_pci_perf_ctr(struct intel_uncore_box * box,int idx)3016bcb2db5SBorislav Petkov unsigned uncore_pci_perf_ctr(struct intel_uncore_box *box, int idx)
3026bcb2db5SBorislav Petkov {
3036bcb2db5SBorislav Petkov return idx * 8 + box->pmu->type->perf_ctr;
3046bcb2db5SBorislav Petkov }
3056bcb2db5SBorislav Petkov
uncore_msr_box_offset(struct intel_uncore_box * box)3066bcb2db5SBorislav Petkov static inline unsigned uncore_msr_box_offset(struct intel_uncore_box *box)
3076bcb2db5SBorislav Petkov {
3086bcb2db5SBorislav Petkov struct intel_uncore_pmu *pmu = box->pmu;
3096bcb2db5SBorislav Petkov return pmu->type->msr_offsets ?
3106bcb2db5SBorislav Petkov pmu->type->msr_offsets[pmu->pmu_idx] :
3116bcb2db5SBorislav Petkov pmu->type->msr_offset * pmu->pmu_idx;
3126bcb2db5SBorislav Petkov }
3136bcb2db5SBorislav Petkov
uncore_msr_box_ctl(struct intel_uncore_box * box)3146bcb2db5SBorislav Petkov static inline unsigned uncore_msr_box_ctl(struct intel_uncore_box *box)
3156bcb2db5SBorislav Petkov {
3166bcb2db5SBorislav Petkov if (!box->pmu->type->box_ctl)
3176bcb2db5SBorislav Petkov return 0;
3186bcb2db5SBorislav Petkov return box->pmu->type->box_ctl + uncore_msr_box_offset(box);
3196bcb2db5SBorislav Petkov }
3206bcb2db5SBorislav Petkov
uncore_msr_fixed_ctl(struct intel_uncore_box * box)3216bcb2db5SBorislav Petkov static inline unsigned uncore_msr_fixed_ctl(struct intel_uncore_box *box)
3226bcb2db5SBorislav Petkov {
3236bcb2db5SBorislav Petkov if (!box->pmu->type->fixed_ctl)
3246bcb2db5SBorislav Petkov return 0;
3256bcb2db5SBorislav Petkov return box->pmu->type->fixed_ctl + uncore_msr_box_offset(box);
3266bcb2db5SBorislav Petkov }
3276bcb2db5SBorislav Petkov
uncore_msr_fixed_ctr(struct intel_uncore_box * box)3286bcb2db5SBorislav Petkov static inline unsigned uncore_msr_fixed_ctr(struct intel_uncore_box *box)
3296bcb2db5SBorislav Petkov {
3306bcb2db5SBorislav Petkov return box->pmu->type->fixed_ctr + uncore_msr_box_offset(box);
3316bcb2db5SBorislav Petkov }
3326bcb2db5SBorislav Petkov
333927b2debSKan Liang
334927b2debSKan Liang /*
335927b2debSKan Liang * In the uncore document, there is no event-code assigned to free running
336927b2debSKan Liang * counters. Some events need to be defined to indicate the free running
337927b2debSKan Liang * counters. The events are encoded as event-code + umask-code.
338927b2debSKan Liang *
339927b2debSKan Liang * The event-code for all free running counters is 0xff, which is the same as
340927b2debSKan Liang * the fixed counters.
341927b2debSKan Liang *
342927b2debSKan Liang * The umask-code is used to distinguish a fixed counter and a free running
343927b2debSKan Liang * counter, and different types of free running counters.
344927b2debSKan Liang * - For fixed counters, the umask-code is 0x0X.
345927b2debSKan Liang * X indicates the index of the fixed counter, which starts from 0.
346927b2debSKan Liang * - For free running counters, the umask-code uses the rest of the space.
347927b2debSKan Liang * It would bare the format of 0xXY.
348927b2debSKan Liang * X stands for the type of free running counters, which starts from 1.
349927b2debSKan Liang * Y stands for the index of free running counters of same type, which
350927b2debSKan Liang * starts from 0.
351927b2debSKan Liang *
352927b2debSKan Liang * For example, there are three types of IIO free running counters on Skylake
353927b2debSKan Liang * server, IO CLOCKS counters, BANDWIDTH counters and UTILIZATION counters.
354927b2debSKan Liang * The event-code for all the free running counters is 0xff.
355927b2debSKan Liang * 'ioclk' is the first counter of IO CLOCKS. IO CLOCKS is the first type,
356927b2debSKan Liang * which umask-code starts from 0x10.
357927b2debSKan Liang * So 'ioclk' is encoded as event=0xff,umask=0x10
358927b2debSKan Liang * 'bw_in_port2' is the third counter of BANDWIDTH counters. BANDWIDTH is
359927b2debSKan Liang * the second type, which umask-code starts from 0x20.
360927b2debSKan Liang * So 'bw_in_port2' is encoded as event=0xff,umask=0x22
361927b2debSKan Liang */
uncore_freerunning_idx(u64 config)362927b2debSKan Liang static inline unsigned int uncore_freerunning_idx(u64 config)
363927b2debSKan Liang {
364927b2debSKan Liang return ((config >> 8) & 0xf);
365927b2debSKan Liang }
366927b2debSKan Liang
367927b2debSKan Liang #define UNCORE_FREERUNNING_UMASK_START 0x10
368927b2debSKan Liang
uncore_freerunning_type(u64 config)369927b2debSKan Liang static inline unsigned int uncore_freerunning_type(u64 config)
370927b2debSKan Liang {
371927b2debSKan Liang return ((((config >> 8) - UNCORE_FREERUNNING_UMASK_START) >> 4) & 0xf);
372927b2debSKan Liang }
373927b2debSKan Liang
374927b2debSKan Liang static inline
uncore_freerunning_counter(struct intel_uncore_box * box,struct perf_event * event)375927b2debSKan Liang unsigned int uncore_freerunning_counter(struct intel_uncore_box *box,
376927b2debSKan Liang struct perf_event *event)
377927b2debSKan Liang {
3788041ffd3SKan Liang unsigned int type = uncore_freerunning_type(event->hw.config);
3798041ffd3SKan Liang unsigned int idx = uncore_freerunning_idx(event->hw.config);
380927b2debSKan Liang struct intel_uncore_pmu *pmu = box->pmu;
381927b2debSKan Liang
382927b2debSKan Liang return pmu->type->freerunning[type].counter_base +
383927b2debSKan Liang pmu->type->freerunning[type].counter_offset * idx +
384bc88a2feSKan Liang (pmu->type->freerunning[type].box_offsets ?
385bc88a2feSKan Liang pmu->type->freerunning[type].box_offsets[pmu->pmu_idx] :
386bc88a2feSKan Liang pmu->type->freerunning[type].box_offset * pmu->pmu_idx);
387927b2debSKan Liang }
388927b2debSKan Liang
3896bcb2db5SBorislav Petkov static inline
uncore_msr_event_ctl(struct intel_uncore_box * box,int idx)3906bcb2db5SBorislav Petkov unsigned uncore_msr_event_ctl(struct intel_uncore_box *box, int idx)
3916bcb2db5SBorislav Petkov {
3924d47d640SKan Liang if (test_bit(UNCORE_BOX_FLAG_CFL8_CBOX_MSR_OFFS, &box->flags)) {
3934d47d640SKan Liang return CFL_UNC_CBO_7_PERFEVTSEL0 +
3944d47d640SKan Liang (box->pmu->type->pair_ctr_ctl ? 2 * idx : idx);
3954d47d640SKan Liang } else {
3966bcb2db5SBorislav Petkov return box->pmu->type->event_ctl +
3976bcb2db5SBorislav Petkov (box->pmu->type->pair_ctr_ctl ? 2 * idx : idx) +
3986bcb2db5SBorislav Petkov uncore_msr_box_offset(box);
3996bcb2db5SBorislav Petkov }
4004d47d640SKan Liang }
4016bcb2db5SBorislav Petkov
4026bcb2db5SBorislav Petkov static inline
uncore_msr_perf_ctr(struct intel_uncore_box * box,int idx)4036bcb2db5SBorislav Petkov unsigned uncore_msr_perf_ctr(struct intel_uncore_box *box, int idx)
4046bcb2db5SBorislav Petkov {
4054d47d640SKan Liang if (test_bit(UNCORE_BOX_FLAG_CFL8_CBOX_MSR_OFFS, &box->flags)) {
4064d47d640SKan Liang return CFL_UNC_CBO_7_PER_CTR0 +
4074d47d640SKan Liang (box->pmu->type->pair_ctr_ctl ? 2 * idx : idx);
4084d47d640SKan Liang } else {
4096bcb2db5SBorislav Petkov return box->pmu->type->perf_ctr +
4106bcb2db5SBorislav Petkov (box->pmu->type->pair_ctr_ctl ? 2 * idx : idx) +
4116bcb2db5SBorislav Petkov uncore_msr_box_offset(box);
4126bcb2db5SBorislav Petkov }
4134d47d640SKan Liang }
4146bcb2db5SBorislav Petkov
4156bcb2db5SBorislav Petkov static inline
uncore_fixed_ctl(struct intel_uncore_box * box)4166bcb2db5SBorislav Petkov unsigned uncore_fixed_ctl(struct intel_uncore_box *box)
4176bcb2db5SBorislav Petkov {
4183da04b8aSKan Liang if (box->pci_dev || box->io_addr)
4196bcb2db5SBorislav Petkov return uncore_pci_fixed_ctl(box);
4206bcb2db5SBorislav Petkov else
4216bcb2db5SBorislav Petkov return uncore_msr_fixed_ctl(box);
4226bcb2db5SBorislav Petkov }
4236bcb2db5SBorislav Petkov
4246bcb2db5SBorislav Petkov static inline
uncore_fixed_ctr(struct intel_uncore_box * box)4256bcb2db5SBorislav Petkov unsigned uncore_fixed_ctr(struct intel_uncore_box *box)
4266bcb2db5SBorislav Petkov {
4273da04b8aSKan Liang if (box->pci_dev || box->io_addr)
4286bcb2db5SBorislav Petkov return uncore_pci_fixed_ctr(box);
4296bcb2db5SBorislav Petkov else
4306bcb2db5SBorislav Petkov return uncore_msr_fixed_ctr(box);
4316bcb2db5SBorislav Petkov }
4326bcb2db5SBorislav Petkov
4336bcb2db5SBorislav Petkov static inline
uncore_event_ctl(struct intel_uncore_box * box,int idx)4346bcb2db5SBorislav Petkov unsigned uncore_event_ctl(struct intel_uncore_box *box, int idx)
4356bcb2db5SBorislav Petkov {
4363da04b8aSKan Liang if (box->pci_dev || box->io_addr)
4376bcb2db5SBorislav Petkov return uncore_pci_event_ctl(box, idx);
4386bcb2db5SBorislav Petkov else
4396bcb2db5SBorislav Petkov return uncore_msr_event_ctl(box, idx);
4406bcb2db5SBorislav Petkov }
4416bcb2db5SBorislav Petkov
4426bcb2db5SBorislav Petkov static inline
uncore_perf_ctr(struct intel_uncore_box * box,int idx)4436bcb2db5SBorislav Petkov unsigned uncore_perf_ctr(struct intel_uncore_box *box, int idx)
4446bcb2db5SBorislav Petkov {
4453da04b8aSKan Liang if (box->pci_dev || box->io_addr)
4466bcb2db5SBorislav Petkov return uncore_pci_perf_ctr(box, idx);
4476bcb2db5SBorislav Petkov else
4486bcb2db5SBorislav Petkov return uncore_msr_perf_ctr(box, idx);
4496bcb2db5SBorislav Petkov }
4506bcb2db5SBorislav Petkov
uncore_perf_ctr_bits(struct intel_uncore_box * box)4516bcb2db5SBorislav Petkov static inline int uncore_perf_ctr_bits(struct intel_uncore_box *box)
4526bcb2db5SBorislav Petkov {
4536bcb2db5SBorislav Petkov return box->pmu->type->perf_ctr_bits;
4546bcb2db5SBorislav Petkov }
4556bcb2db5SBorislav Petkov
uncore_fixed_ctr_bits(struct intel_uncore_box * box)4566bcb2db5SBorislav Petkov static inline int uncore_fixed_ctr_bits(struct intel_uncore_box *box)
4576bcb2db5SBorislav Petkov {
4586bcb2db5SBorislav Petkov return box->pmu->type->fixed_ctr_bits;
4596bcb2db5SBorislav Petkov }
4606bcb2db5SBorislav Petkov
461927b2debSKan Liang static inline
uncore_freerunning_bits(struct intel_uncore_box * box,struct perf_event * event)462927b2debSKan Liang unsigned int uncore_freerunning_bits(struct intel_uncore_box *box,
463927b2debSKan Liang struct perf_event *event)
464927b2debSKan Liang {
4658041ffd3SKan Liang unsigned int type = uncore_freerunning_type(event->hw.config);
466927b2debSKan Liang
467927b2debSKan Liang return box->pmu->type->freerunning[type].bits;
468927b2debSKan Liang }
469927b2debSKan Liang
uncore_num_freerunning(struct intel_uncore_box * box,struct perf_event * event)470927b2debSKan Liang static inline int uncore_num_freerunning(struct intel_uncore_box *box,
471927b2debSKan Liang struct perf_event *event)
472927b2debSKan Liang {
4738041ffd3SKan Liang unsigned int type = uncore_freerunning_type(event->hw.config);
474927b2debSKan Liang
475927b2debSKan Liang return box->pmu->type->freerunning[type].num_counters;
476927b2debSKan Liang }
477927b2debSKan Liang
uncore_num_freerunning_types(struct intel_uncore_box * box,struct perf_event * event)478927b2debSKan Liang static inline int uncore_num_freerunning_types(struct intel_uncore_box *box,
479927b2debSKan Liang struct perf_event *event)
480927b2debSKan Liang {
481927b2debSKan Liang return box->pmu->type->num_freerunning_types;
482927b2debSKan Liang }
483927b2debSKan Liang
check_valid_freerunning_event(struct intel_uncore_box * box,struct perf_event * event)484927b2debSKan Liang static inline bool check_valid_freerunning_event(struct intel_uncore_box *box,
485927b2debSKan Liang struct perf_event *event)
486927b2debSKan Liang {
4878041ffd3SKan Liang unsigned int type = uncore_freerunning_type(event->hw.config);
4888041ffd3SKan Liang unsigned int idx = uncore_freerunning_idx(event->hw.config);
489927b2debSKan Liang
490927b2debSKan Liang return (type < uncore_num_freerunning_types(box, event)) &&
491927b2debSKan Liang (idx < uncore_num_freerunning(box, event));
492927b2debSKan Liang }
493927b2debSKan Liang
uncore_num_counters(struct intel_uncore_box * box)4946bcb2db5SBorislav Petkov static inline int uncore_num_counters(struct intel_uncore_box *box)
4956bcb2db5SBorislav Petkov {
4966bcb2db5SBorislav Petkov return box->pmu->type->num_counters;
4976bcb2db5SBorislav Petkov }
4986bcb2db5SBorislav Petkov
is_freerunning_event(struct perf_event * event)499927b2debSKan Liang static inline bool is_freerunning_event(struct perf_event *event)
500927b2debSKan Liang {
501927b2debSKan Liang u64 cfg = event->attr.config;
502927b2debSKan Liang
503927b2debSKan Liang return ((cfg & UNCORE_FIXED_EVENT) == UNCORE_FIXED_EVENT) &&
504927b2debSKan Liang (((cfg >> 8) & 0xff) >= UNCORE_FREERUNNING_UMASK_START);
505927b2debSKan Liang }
506927b2debSKan Liang
507543ac280SKan Liang /* Check and reject invalid config */
uncore_freerunning_hw_config(struct intel_uncore_box * box,struct perf_event * event)508543ac280SKan Liang static inline int uncore_freerunning_hw_config(struct intel_uncore_box *box,
509543ac280SKan Liang struct perf_event *event)
510543ac280SKan Liang {
511543ac280SKan Liang if (is_freerunning_event(event))
512543ac280SKan Liang return 0;
513543ac280SKan Liang
514543ac280SKan Liang return -EINVAL;
515543ac280SKan Liang }
516543ac280SKan Liang
uncore_disable_event(struct intel_uncore_box * box,struct perf_event * event)5176bcb2db5SBorislav Petkov static inline void uncore_disable_event(struct intel_uncore_box *box,
5186bcb2db5SBorislav Petkov struct perf_event *event)
5196bcb2db5SBorislav Petkov {
5206bcb2db5SBorislav Petkov box->pmu->type->ops->disable_event(box, event);
5216bcb2db5SBorislav Petkov }
5226bcb2db5SBorislav Petkov
uncore_enable_event(struct intel_uncore_box * box,struct perf_event * event)5236bcb2db5SBorislav Petkov static inline void uncore_enable_event(struct intel_uncore_box *box,
5246bcb2db5SBorislav Petkov struct perf_event *event)
5256bcb2db5SBorislav Petkov {
5266bcb2db5SBorislav Petkov box->pmu->type->ops->enable_event(box, event);
5276bcb2db5SBorislav Petkov }
5286bcb2db5SBorislav Petkov
uncore_read_counter(struct intel_uncore_box * box,struct perf_event * event)5296bcb2db5SBorislav Petkov static inline u64 uncore_read_counter(struct intel_uncore_box *box,
5306bcb2db5SBorislav Petkov struct perf_event *event)
5316bcb2db5SBorislav Petkov {
5326bcb2db5SBorislav Petkov return box->pmu->type->ops->read_counter(box, event);
5336bcb2db5SBorislav Petkov }
5346bcb2db5SBorislav Petkov
uncore_box_init(struct intel_uncore_box * box)5356bcb2db5SBorislav Petkov static inline void uncore_box_init(struct intel_uncore_box *box)
5366bcb2db5SBorislav Petkov {
5376bcb2db5SBorislav Petkov if (!test_and_set_bit(UNCORE_BOX_FLAG_INITIATED, &box->flags)) {
5386bcb2db5SBorislav Petkov if (box->pmu->type->ops->init_box)
5396bcb2db5SBorislav Petkov box->pmu->type->ops->init_box(box);
5406bcb2db5SBorislav Petkov }
5416bcb2db5SBorislav Petkov }
5426bcb2db5SBorislav Petkov
uncore_box_exit(struct intel_uncore_box * box)543a46195f1SThomas Gleixner static inline void uncore_box_exit(struct intel_uncore_box *box)
544a46195f1SThomas Gleixner {
545a46195f1SThomas Gleixner if (test_and_clear_bit(UNCORE_BOX_FLAG_INITIATED, &box->flags)) {
546a46195f1SThomas Gleixner if (box->pmu->type->ops->exit_box)
547a46195f1SThomas Gleixner box->pmu->type->ops->exit_box(box);
548a46195f1SThomas Gleixner }
549a46195f1SThomas Gleixner }
550a46195f1SThomas Gleixner
uncore_box_is_fake(struct intel_uncore_box * box)5516bcb2db5SBorislav Petkov static inline bool uncore_box_is_fake(struct intel_uncore_box *box)
5526bcb2db5SBorislav Petkov {
553b0529b9cSKan Liang return (box->dieid < 0);
5546bcb2db5SBorislav Petkov }
5556bcb2db5SBorislav Petkov
uncore_event_to_pmu(struct perf_event * event)5561f2569faSThomas Gleixner static inline struct intel_uncore_pmu *uncore_event_to_pmu(struct perf_event *event)
5571f2569faSThomas Gleixner {
5581f2569faSThomas Gleixner return container_of(event->pmu, struct intel_uncore_pmu, pmu);
5591f2569faSThomas Gleixner }
5601f2569faSThomas Gleixner
uncore_event_to_box(struct perf_event * event)5611f2569faSThomas Gleixner static inline struct intel_uncore_box *uncore_event_to_box(struct perf_event *event)
5621f2569faSThomas Gleixner {
5631f2569faSThomas Gleixner return event->pmu_private;
5641f2569faSThomas Gleixner }
5651f2569faSThomas Gleixner
5666bcb2db5SBorislav Petkov struct intel_uncore_box *uncore_pmu_to_box(struct intel_uncore_pmu *pmu, int cpu);
5676bcb2db5SBorislav Petkov u64 uncore_msr_read_counter(struct intel_uncore_box *box, struct perf_event *event);
56807ce734dSKan Liang void uncore_mmio_exit_box(struct intel_uncore_box *box);
56907ce734dSKan Liang u64 uncore_mmio_read_counter(struct intel_uncore_box *box,
57007ce734dSKan Liang struct perf_event *event);
5716bcb2db5SBorislav Petkov void uncore_pmu_start_hrtimer(struct intel_uncore_box *box);
5726bcb2db5SBorislav Petkov void uncore_pmu_cancel_hrtimer(struct intel_uncore_box *box);
5735a6c9d94SKan Liang void uncore_pmu_event_start(struct perf_event *event, int flags);
5745a6c9d94SKan Liang void uncore_pmu_event_stop(struct perf_event *event, int flags);
5755a6c9d94SKan Liang int uncore_pmu_event_add(struct perf_event *event, int flags);
5765a6c9d94SKan Liang void uncore_pmu_event_del(struct perf_event *event, int flags);
5776bcb2db5SBorislav Petkov void uncore_pmu_event_read(struct perf_event *event);
5786bcb2db5SBorislav Petkov void uncore_perf_event_update(struct intel_uncore_box *box, struct perf_event *event);
5796bcb2db5SBorislav Petkov struct event_constraint *
5806bcb2db5SBorislav Petkov uncore_get_constraint(struct intel_uncore_box *box, struct perf_event *event);
5816bcb2db5SBorislav Petkov void uncore_put_constraint(struct intel_uncore_box *box, struct perf_event *event);
5826bcb2db5SBorislav Petkov u64 uncore_shared_reg_config(struct intel_uncore_box *box, int idx);
5838053f2d7SKan Liang void uncore_get_alias_name(char *pmu_name, struct intel_uncore_pmu *pmu);
5846bcb2db5SBorislav Petkov
585d6c75413SKan Liang extern struct intel_uncore_type *empty_uncore[];
5866bcb2db5SBorislav Petkov extern struct intel_uncore_type **uncore_msr_uncores;
5876bcb2db5SBorislav Petkov extern struct intel_uncore_type **uncore_pci_uncores;
5883da04b8aSKan Liang extern struct intel_uncore_type **uncore_mmio_uncores;
5896bcb2db5SBorislav Petkov extern struct pci_driver *uncore_pci_driver;
59095a7fc77SKan Liang extern struct pci_driver *uncore_pci_sub_driver;
5916bcb2db5SBorislav Petkov extern raw_spinlock_t pci2phy_map_lock;
5926bcb2db5SBorislav Petkov extern struct list_head pci2phy_map_head;
593cf6d445fSThomas Gleixner extern struct pci_extra_dev *uncore_extra_pci_dev;
5946bcb2db5SBorislav Petkov extern struct event_constraint uncore_constraint_empty;
59565248a9aSKan Liang extern int spr_uncore_units_ignore[];
5966bcb2db5SBorislav Petkov
597940b2f2fSBorislav Petkov /* uncore_snb.c */
5986bcb2db5SBorislav Petkov int snb_uncore_pci_init(void);
5996bcb2db5SBorislav Petkov int ivb_uncore_pci_init(void);
6006bcb2db5SBorislav Petkov int hsw_uncore_pci_init(void);
6016bcb2db5SBorislav Petkov int bdw_uncore_pci_init(void);
6026bcb2db5SBorislav Petkov int skl_uncore_pci_init(void);
6036bcb2db5SBorislav Petkov void snb_uncore_cpu_init(void);
6046bcb2db5SBorislav Petkov void nhm_uncore_cpu_init(void);
60546866b59SKan Liang void skl_uncore_cpu_init(void);
6066e394376SKan Liang void icl_uncore_cpu_init(void);
6078abbcfefSKan Liang void tgl_uncore_cpu_init(void);
6085a4487f9SKan Liang void adl_uncore_cpu_init(void);
609*c828441fSKan Liang void mtl_uncore_cpu_init(void);
610fdb64822SKan Liang void tgl_uncore_mmio_init(void);
611fdb64822SKan Liang void tgl_l_uncore_mmio_init(void);
6125a4487f9SKan Liang void adl_uncore_mmio_init(void);
6136bcb2db5SBorislav Petkov int snb_pci2phy_map_init(int devid);
6146bcb2db5SBorislav Petkov
615940b2f2fSBorislav Petkov /* uncore_snbep.c */
6166bcb2db5SBorislav Petkov int snbep_uncore_pci_init(void);
6176bcb2db5SBorislav Petkov void snbep_uncore_cpu_init(void);
6186bcb2db5SBorislav Petkov int ivbep_uncore_pci_init(void);
6196bcb2db5SBorislav Petkov void ivbep_uncore_cpu_init(void);
6206bcb2db5SBorislav Petkov int hswep_uncore_pci_init(void);
6216bcb2db5SBorislav Petkov void hswep_uncore_cpu_init(void);
6226bcb2db5SBorislav Petkov int bdx_uncore_pci_init(void);
6236bcb2db5SBorislav Petkov void bdx_uncore_cpu_init(void);
6246bcb2db5SBorislav Petkov int knl_uncore_pci_init(void);
6256bcb2db5SBorislav Petkov void knl_uncore_cpu_init(void);
626cd34cd97SKan Liang int skx_uncore_pci_init(void);
627cd34cd97SKan Liang void skx_uncore_cpu_init(void);
628210cc5f9SKan Liang int snr_uncore_pci_init(void);
629210cc5f9SKan Liang void snr_uncore_cpu_init(void);
630ee49532bSKan Liang void snr_uncore_mmio_init(void);
6312b3b76b5SKan Liang int icx_uncore_pci_init(void);
6322b3b76b5SKan Liang void icx_uncore_cpu_init(void);
6332b3b76b5SKan Liang void icx_uncore_mmio_init(void);
634c54c53d9SKan Liang int spr_uncore_pci_init(void);
635c54c53d9SKan Liang void spr_uncore_cpu_init(void);
636c54c53d9SKan Liang void spr_uncore_mmio_init(void);
6376bcb2db5SBorislav Petkov
638940b2f2fSBorislav Petkov /* uncore_nhmex.c */
6396bcb2db5SBorislav Petkov void nhmex_uncore_cpu_init(void);
640