xref: /openbmc/linux/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.h (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1c315669eSAthira Rajeev /* SPDX-License-Identifier: GPL-2.0-only */
2c315669eSAthira Rajeev /*
3c315669eSAthira Rajeev  * Copyright 2022, Athira Rajeev, IBM Corp.
46523dce8SMadhavan Srinivasan  * Copyright 2022, Madhavan Srinivasan, IBM Corp.
55f6c3061SKajol Jain  * Copyright 2022, Kajol Jain, IBM Corp.
6c315669eSAthira Rajeev  */
7c315669eSAthira Rajeev 
8a069b5f9SAthira Rajeev #include <sys/stat.h>
9c315669eSAthira Rajeev #include "../event.h"
10c315669eSAthira Rajeev 
116523dce8SMadhavan Srinivasan #define POWER10 0x80
126523dce8SMadhavan Srinivasan #define POWER9  0x4e
136523dce8SMadhavan Srinivasan #define PERF_POWER9_MASK        0x7f8ffffffffffff
146523dce8SMadhavan Srinivasan #define PERF_POWER10_MASK       0x7ffffffffffffff
156523dce8SMadhavan Srinivasan 
162b49e641SAthira Rajeev #define MMCR0_FC56      0x00000010UL /* freeze counters 5 and 6 */
172b49e641SAthira Rajeev #define MMCR0_PMCCEXT   0x00000200UL /* PMCCEXT control */
182b49e641SAthira Rajeev #define MMCR1_RSQ       0x200000000000ULL /* radix scope qual field */
1913307f95SKajol Jain #define BHRB_DISABLE    0x2000000000ULL /* MMCRA BHRB DISABLE bit */
202b49e641SAthira Rajeev 
216523dce8SMadhavan Srinivasan extern int ev_mask_pmcxsel, ev_shift_pmcxsel;
226523dce8SMadhavan Srinivasan extern int ev_mask_marked, ev_shift_marked;
236523dce8SMadhavan Srinivasan extern int ev_mask_comb, ev_shift_comb;
246523dce8SMadhavan Srinivasan extern int ev_mask_unit, ev_shift_unit;
256523dce8SMadhavan Srinivasan extern int ev_mask_pmc, ev_shift_pmc;
266523dce8SMadhavan Srinivasan extern int ev_mask_cache, ev_shift_cache;
276523dce8SMadhavan Srinivasan extern int ev_mask_sample, ev_shift_sample;
286523dce8SMadhavan Srinivasan extern int ev_mask_thd_sel, ev_shift_thd_sel;
296523dce8SMadhavan Srinivasan extern int ev_mask_thd_start, ev_shift_thd_start;
306523dce8SMadhavan Srinivasan extern int ev_mask_thd_stop, ev_shift_thd_stop;
316523dce8SMadhavan Srinivasan extern int ev_mask_thd_cmp, ev_shift_thd_cmp;
326523dce8SMadhavan Srinivasan extern int ev_mask_sm, ev_shift_sm;
336523dce8SMadhavan Srinivasan extern int ev_mask_rsq, ev_shift_rsq;
346523dce8SMadhavan Srinivasan extern int ev_mask_l2l3, ev_shift_l2l3;
356523dce8SMadhavan Srinivasan extern int ev_mask_mmcr3_src, ev_shift_mmcr3_src;
366523dce8SMadhavan Srinivasan extern int pvr;
376523dce8SMadhavan Srinivasan extern u64 platform_extended_mask;
386523dce8SMadhavan Srinivasan extern int check_pvr_for_sampling_tests(void);
39*9cfd110aSAthira Rajeev extern int platform_check_for_tests(void);
406523dce8SMadhavan Srinivasan 
416523dce8SMadhavan Srinivasan /*
426523dce8SMadhavan Srinivasan  * Event code field extraction macro.
436523dce8SMadhavan Srinivasan  * Raw event code is combination of multiple
446523dce8SMadhavan Srinivasan  * fields. Macro to extract individual fields
456523dce8SMadhavan Srinivasan  *
466523dce8SMadhavan Srinivasan  * x - Raw event code value
476523dce8SMadhavan Srinivasan  * y - Field to extract
486523dce8SMadhavan Srinivasan  */
496523dce8SMadhavan Srinivasan #define EV_CODE_EXTRACT(x, y)   \
506523dce8SMadhavan Srinivasan 	((x >> ev_shift_##y) & ev_mask_##y)
516523dce8SMadhavan Srinivasan 
52c315669eSAthira Rajeev void *event_sample_buf_mmap(int fd, int mmap_pages);
53c315669eSAthira Rajeev void *__event_read_samples(void *sample_buff, size_t *size, u64 *sample_count);
545f6c3061SKajol Jain int collect_samples(void *sample_buff);
555f6c3061SKajol Jain u64 *get_intr_regs(struct event *event, void *sample_buff);
565f6c3061SKajol Jain u64 get_reg_value(u64 *intr_regs, char *register_name);
5742e0576eSKajol Jain int get_thresh_cmp_val(struct event event);
58a069b5f9SAthira Rajeev bool check_for_generic_compat_pmu(void);
59a069b5f9SAthira Rajeev bool check_for_compat_mode(void);
6079c4e6abSMadhavan Srinivasan 
get_mmcr0_fc56(u64 mmcr0,int pmc)612b49e641SAthira Rajeev static inline int get_mmcr0_fc56(u64 mmcr0, int pmc)
622b49e641SAthira Rajeev {
632b49e641SAthira Rajeev 	return (mmcr0 & MMCR0_FC56);
642b49e641SAthira Rajeev }
652b49e641SAthira Rajeev 
get_mmcr0_pmccext(u64 mmcr0,int pmc)662b49e641SAthira Rajeev static inline int get_mmcr0_pmccext(u64 mmcr0, int pmc)
672b49e641SAthira Rajeev {
682b49e641SAthira Rajeev 	return (mmcr0 & MMCR0_PMCCEXT);
692b49e641SAthira Rajeev }
702b49e641SAthira Rajeev 
get_mmcr0_pmao(u64 mmcr0,int pmc)712b49e641SAthira Rajeev static inline int get_mmcr0_pmao(u64 mmcr0, int pmc)
722b49e641SAthira Rajeev {
732b49e641SAthira Rajeev 	return ((mmcr0 >> 7) & 0x1);
742b49e641SAthira Rajeev }
752b49e641SAthira Rajeev 
get_mmcr0_cc56run(u64 mmcr0,int pmc)762b49e641SAthira Rajeev static inline int get_mmcr0_cc56run(u64 mmcr0, int pmc)
772b49e641SAthira Rajeev {
782b49e641SAthira Rajeev 	return ((mmcr0 >> 8) & 0x1);
792b49e641SAthira Rajeev }
802b49e641SAthira Rajeev 
get_mmcr0_pmcjce(u64 mmcr0,int pmc)812b49e641SAthira Rajeev static inline int get_mmcr0_pmcjce(u64 mmcr0, int pmc)
822b49e641SAthira Rajeev {
832b49e641SAthira Rajeev 	return ((mmcr0 >> 14) & 0x1);
842b49e641SAthira Rajeev }
852b49e641SAthira Rajeev 
get_mmcr0_pmc1ce(u64 mmcr0,int pmc)862b49e641SAthira Rajeev static inline int get_mmcr0_pmc1ce(u64 mmcr0, int pmc)
872b49e641SAthira Rajeev {
882b49e641SAthira Rajeev 	return ((mmcr0 >> 15) & 0x1);
892b49e641SAthira Rajeev }
902b49e641SAthira Rajeev 
get_mmcr0_pmae(u64 mmcr0,int pmc)912b49e641SAthira Rajeev static inline int get_mmcr0_pmae(u64 mmcr0, int pmc)
922b49e641SAthira Rajeev {
932b49e641SAthira Rajeev 	return ((mmcr0 >> 27) & 0x1);
942b49e641SAthira Rajeev }
952b49e641SAthira Rajeev 
get_mmcr1_pmcxsel(u64 mmcr1,int pmc)962b49e641SAthira Rajeev static inline int get_mmcr1_pmcxsel(u64 mmcr1, int pmc)
972b49e641SAthira Rajeev {
982b49e641SAthira Rajeev 	return ((mmcr1 >> ((24 - (((pmc) - 1) * 8))) & 0xff));
992b49e641SAthira Rajeev }
1002b49e641SAthira Rajeev 
get_mmcr1_unit(u64 mmcr1,int pmc)1012b49e641SAthira Rajeev static inline int get_mmcr1_unit(u64 mmcr1, int pmc)
1022b49e641SAthira Rajeev {
1032b49e641SAthira Rajeev 	return ((mmcr1 >> ((60 - (4 * ((pmc) - 1))))) & 0xf);
1042b49e641SAthira Rajeev }
1052b49e641SAthira Rajeev 
get_mmcr1_comb(u64 mmcr1,int pmc)1062b49e641SAthira Rajeev static inline int get_mmcr1_comb(u64 mmcr1, int pmc)
1072b49e641SAthira Rajeev {
1082b49e641SAthira Rajeev 	return ((mmcr1 >> (38 - ((pmc - 1) * 2))) & 0x3);
1092b49e641SAthira Rajeev }
1102b49e641SAthira Rajeev 
get_mmcr1_cache(u64 mmcr1,int pmc)1112b49e641SAthira Rajeev static inline int get_mmcr1_cache(u64 mmcr1, int pmc)
1122b49e641SAthira Rajeev {
1132b49e641SAthira Rajeev 	return ((mmcr1 >> 46) & 0x3);
1142b49e641SAthira Rajeev }
1152b49e641SAthira Rajeev 
get_mmcr1_rsq(u64 mmcr1,int pmc)1162b49e641SAthira Rajeev static inline int get_mmcr1_rsq(u64 mmcr1, int pmc)
1172b49e641SAthira Rajeev {
1182b49e641SAthira Rajeev 	return mmcr1 & MMCR1_RSQ;
1192b49e641SAthira Rajeev }
1202b49e641SAthira Rajeev 
get_mmcr2_fcs(u64 mmcr2,int pmc)12179c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fcs(u64 mmcr2, int pmc)
12279c4e6abSMadhavan Srinivasan {
12379c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (63 - (((pmc) - 1) * 9)))) >> (63 - (((pmc) - 1) * 9)));
12479c4e6abSMadhavan Srinivasan }
12579c4e6abSMadhavan Srinivasan 
get_mmcr2_fcp(u64 mmcr2,int pmc)12679c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fcp(u64 mmcr2, int pmc)
12779c4e6abSMadhavan Srinivasan {
12879c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (62 - (((pmc) - 1) * 9)))) >> (62 - (((pmc) - 1) * 9)));
12979c4e6abSMadhavan Srinivasan }
13079c4e6abSMadhavan Srinivasan 
get_mmcr2_fcpc(u64 mmcr2,int pmc)13179c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fcpc(u64 mmcr2, int pmc)
13279c4e6abSMadhavan Srinivasan {
13379c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (61 - (((pmc) - 1) * 9)))) >> (61 - (((pmc) - 1) * 9)));
13479c4e6abSMadhavan Srinivasan }
13579c4e6abSMadhavan Srinivasan 
get_mmcr2_fcm1(u64 mmcr2,int pmc)13679c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fcm1(u64 mmcr2, int pmc)
13779c4e6abSMadhavan Srinivasan {
13879c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (60 - (((pmc) - 1) * 9)))) >> (60 - (((pmc) - 1) * 9)));
13979c4e6abSMadhavan Srinivasan }
14079c4e6abSMadhavan Srinivasan 
get_mmcr2_fcm0(u64 mmcr2,int pmc)14179c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fcm0(u64 mmcr2, int pmc)
14279c4e6abSMadhavan Srinivasan {
14379c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (59 - (((pmc) - 1) * 9)))) >> (59 - (((pmc) - 1) * 9)));
14479c4e6abSMadhavan Srinivasan }
14579c4e6abSMadhavan Srinivasan 
get_mmcr2_fcwait(u64 mmcr2,int pmc)14679c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fcwait(u64 mmcr2, int pmc)
14779c4e6abSMadhavan Srinivasan {
14879c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (58 - (((pmc) - 1) * 9)))) >> (58 - (((pmc) - 1) * 9)));
14979c4e6abSMadhavan Srinivasan }
15079c4e6abSMadhavan Srinivasan 
get_mmcr2_fch(u64 mmcr2,int pmc)15179c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fch(u64 mmcr2, int pmc)
15279c4e6abSMadhavan Srinivasan {
15379c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (57 - (((pmc) - 1) * 9)))) >> (57 - (((pmc) - 1) * 9)));
15479c4e6abSMadhavan Srinivasan }
15579c4e6abSMadhavan Srinivasan 
get_mmcr2_fcti(u64 mmcr2,int pmc)15679c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fcti(u64 mmcr2, int pmc)
15779c4e6abSMadhavan Srinivasan {
15879c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (56 - (((pmc) - 1) * 9)))) >> (56 - (((pmc) - 1) * 9)));
15979c4e6abSMadhavan Srinivasan }
16079c4e6abSMadhavan Srinivasan 
get_mmcr2_fcta(u64 mmcr2,int pmc)16179c4e6abSMadhavan Srinivasan static inline int get_mmcr2_fcta(u64 mmcr2, int pmc)
16279c4e6abSMadhavan Srinivasan {
16379c4e6abSMadhavan Srinivasan 	return ((mmcr2 & (1ull << (55 - (((pmc) - 1) * 9)))) >> (55 - (((pmc) - 1) * 9)));
16479c4e6abSMadhavan Srinivasan }
16579c4e6abSMadhavan Srinivasan 
get_mmcr2_l2l3(u64 mmcr2,int pmc)16679c4e6abSMadhavan Srinivasan static inline int get_mmcr2_l2l3(u64 mmcr2, int pmc)
16779c4e6abSMadhavan Srinivasan {
16879c4e6abSMadhavan Srinivasan 	if (pvr == POWER10)
16979c4e6abSMadhavan Srinivasan 		return ((mmcr2 & 0xf8) >> 3);
17079c4e6abSMadhavan Srinivasan 	return 0;
17179c4e6abSMadhavan Srinivasan }
17213307f95SKajol Jain 
get_mmcr3_src(u64 mmcr3,int pmc)17313307f95SKajol Jain static inline int get_mmcr3_src(u64 mmcr3, int pmc)
17413307f95SKajol Jain {
17513307f95SKajol Jain 	if (pvr != POWER10)
17613307f95SKajol Jain 		return 0;
17713307f95SKajol Jain 	return ((mmcr3 >> ((49 - (15 * ((pmc) - 1))))) & 0x7fff);
17813307f95SKajol Jain }
17913307f95SKajol Jain 
get_mmcra_thd_cmp(u64 mmcra,int pmc)18013307f95SKajol Jain static inline int get_mmcra_thd_cmp(u64 mmcra, int pmc)
18113307f95SKajol Jain {
18213307f95SKajol Jain 	if (pvr == POWER10)
18313307f95SKajol Jain 		return ((mmcra >> 45) & 0x7ff);
18413307f95SKajol Jain 	return ((mmcra >> 45) & 0x3ff);
18513307f95SKajol Jain }
18613307f95SKajol Jain 
get_mmcra_sm(u64 mmcra,int pmc)18713307f95SKajol Jain static inline int get_mmcra_sm(u64 mmcra, int pmc)
18813307f95SKajol Jain {
18913307f95SKajol Jain 	return ((mmcra >> 42) & 0x3);
19013307f95SKajol Jain }
19113307f95SKajol Jain 
get_mmcra_bhrb_disable(u64 mmcra,int pmc)19284cc4e66SKajol Jain static inline u64 get_mmcra_bhrb_disable(u64 mmcra, int pmc)
19313307f95SKajol Jain {
19413307f95SKajol Jain 	if (pvr == POWER10)
19513307f95SKajol Jain 		return mmcra & BHRB_DISABLE;
19613307f95SKajol Jain 	return 0;
19713307f95SKajol Jain }
19813307f95SKajol Jain 
get_mmcra_ifm(u64 mmcra,int pmc)19913307f95SKajol Jain static inline int get_mmcra_ifm(u64 mmcra, int pmc)
20013307f95SKajol Jain {
20113307f95SKajol Jain 	return ((mmcra >> 30) & 0x3);
20213307f95SKajol Jain }
20313307f95SKajol Jain 
get_mmcra_thd_sel(u64 mmcra,int pmc)20413307f95SKajol Jain static inline int get_mmcra_thd_sel(u64 mmcra, int pmc)
20513307f95SKajol Jain {
20613307f95SKajol Jain 	return ((mmcra >> 16) & 0x7);
20713307f95SKajol Jain }
20813307f95SKajol Jain 
get_mmcra_thd_start(u64 mmcra,int pmc)20913307f95SKajol Jain static inline int get_mmcra_thd_start(u64 mmcra, int pmc)
21013307f95SKajol Jain {
21113307f95SKajol Jain 	return ((mmcra >> 12) & 0xf);
21213307f95SKajol Jain }
21313307f95SKajol Jain 
get_mmcra_thd_stop(u64 mmcra,int pmc)21413307f95SKajol Jain static inline int get_mmcra_thd_stop(u64 mmcra, int pmc)
21513307f95SKajol Jain {
21613307f95SKajol Jain 	return ((mmcra >> 8) & 0xf);
21713307f95SKajol Jain }
21813307f95SKajol Jain 
get_mmcra_rand_samp_elig(u64 mmcra,int pmc)21913307f95SKajol Jain static inline int get_mmcra_rand_samp_elig(u64 mmcra, int pmc)
22013307f95SKajol Jain {
22113307f95SKajol Jain 	return ((mmcra >> 4) & 0x7);
22213307f95SKajol Jain }
22313307f95SKajol Jain 
get_mmcra_sample_mode(u64 mmcra,int pmc)22413307f95SKajol Jain static inline int get_mmcra_sample_mode(u64 mmcra, int pmc)
22513307f95SKajol Jain {
22613307f95SKajol Jain 	return ((mmcra >> 1) & 0x3);
22713307f95SKajol Jain }
22813307f95SKajol Jain 
get_mmcra_marked(u64 mmcra,int pmc)22913307f95SKajol Jain static inline int get_mmcra_marked(u64 mmcra, int pmc)
23013307f95SKajol Jain {
23113307f95SKajol Jain 	return mmcra & 0x1;
23213307f95SKajol Jain }
233