18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Performance counter support for e500 family processors.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
68c2ecf20Sopenharmony_ci * Copyright 2010 Freescale Semiconductor, Inc.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/string.h>
98c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
108c2ecf20Sopenharmony_ci#include <asm/reg.h>
118c2ecf20Sopenharmony_ci#include <asm/cputable.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * Map of generic hardware event types to hardware events
158c2ecf20Sopenharmony_ci * Zero if unsupported
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_cistatic int e500_generic_events[] = {
188c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CPU_CYCLES] = 1,
198c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_INSTRUCTIONS] = 2,
208c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_MISSES] = 41, /* Data L1 cache reloads */
218c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 12,
228c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_MISSES] = 15,
238c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 18,
248c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 19,
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define C(x)	PERF_COUNT_HW_CACHE_##x
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * Table of generalized cache-related events.
318c2ecf20Sopenharmony_ci * 0 means not supported, -1 means nonsensical, other values
328c2ecf20Sopenharmony_ci * are event codes.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_cistatic int e500_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
358c2ecf20Sopenharmony_ci	/*
368c2ecf20Sopenharmony_ci	 * D-cache misses are not split into read/write/prefetch;
378c2ecf20Sopenharmony_ci	 * use raw event 41.
388c2ecf20Sopenharmony_ci	 */
398c2ecf20Sopenharmony_ci	[C(L1D)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
408c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	27,		0	},
418c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	28,		0	},
428c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	29,		0	},
438c2ecf20Sopenharmony_ci	},
448c2ecf20Sopenharmony_ci	[C(L1I)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
458c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	2,		60	},
468c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
478c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0,		0	},
488c2ecf20Sopenharmony_ci	},
498c2ecf20Sopenharmony_ci	/*
508c2ecf20Sopenharmony_ci	 * Assuming LL means L2, it's not a good match for this model.
518c2ecf20Sopenharmony_ci	 * It allocates only on L1 castout or explicit prefetch, and
528c2ecf20Sopenharmony_ci	 * does not have separate read/write events (but it does have
538c2ecf20Sopenharmony_ci	 * separate instruction/data events).
548c2ecf20Sopenharmony_ci	 */
558c2ecf20Sopenharmony_ci	[C(LL)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
568c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0	},
578c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0,		0	},
588c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0,		0	},
598c2ecf20Sopenharmony_ci	},
608c2ecf20Sopenharmony_ci	/*
618c2ecf20Sopenharmony_ci	 * There are data/instruction MMU misses, but that's a miss on
628c2ecf20Sopenharmony_ci	 * the chip's internal level-one TLB which is probably not
638c2ecf20Sopenharmony_ci	 * what the user wants.  Instead, unified level-two TLB misses
648c2ecf20Sopenharmony_ci	 * are reported here.
658c2ecf20Sopenharmony_ci	 */
668c2ecf20Sopenharmony_ci	[C(DTLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
678c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	26,		66	},
688c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
698c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
708c2ecf20Sopenharmony_ci	},
718c2ecf20Sopenharmony_ci	[C(BPU)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
728c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	12,		15 	},
738c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
748c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
758c2ecf20Sopenharmony_ci	},
768c2ecf20Sopenharmony_ci	[C(NODE)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
778c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	-1,		-1 	},
788c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
798c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
808c2ecf20Sopenharmony_ci	},
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic int num_events = 128;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* Upper half of event id is PMLCb, for threshold events */
868c2ecf20Sopenharmony_cistatic u64 e500_xlate_event(u64 event_id)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	u32 event_low = (u32)event_id;
898c2ecf20Sopenharmony_ci	u64 ret;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (event_low >= num_events)
928c2ecf20Sopenharmony_ci		return 0;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	ret = FSL_EMB_EVENT_VALID;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (event_low >= 76 && event_low <= 81) {
978c2ecf20Sopenharmony_ci		ret |= FSL_EMB_EVENT_RESTRICTED;
988c2ecf20Sopenharmony_ci		ret |= event_id &
998c2ecf20Sopenharmony_ci		       (FSL_EMB_EVENT_THRESHMUL | FSL_EMB_EVENT_THRESH);
1008c2ecf20Sopenharmony_ci	} else if (event_id &
1018c2ecf20Sopenharmony_ci	           (FSL_EMB_EVENT_THRESHMUL | FSL_EMB_EVENT_THRESH)) {
1028c2ecf20Sopenharmony_ci		/* Threshold requested on non-threshold event */
1038c2ecf20Sopenharmony_ci		return 0;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	return ret;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic struct fsl_emb_pmu e500_pmu = {
1108c2ecf20Sopenharmony_ci	.name			= "e500 family",
1118c2ecf20Sopenharmony_ci	.n_counter		= 4,
1128c2ecf20Sopenharmony_ci	.n_restricted		= 2,
1138c2ecf20Sopenharmony_ci	.xlate_event		= e500_xlate_event,
1148c2ecf20Sopenharmony_ci	.n_generic		= ARRAY_SIZE(e500_generic_events),
1158c2ecf20Sopenharmony_ci	.generic_events		= e500_generic_events,
1168c2ecf20Sopenharmony_ci	.cache_events		= &e500_cache_events,
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int init_e500_pmu(void)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	if (!cur_cpu_spec->oprofile_cpu_type)
1228c2ecf20Sopenharmony_ci		return -ENODEV;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (!strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc/e500mc"))
1258c2ecf20Sopenharmony_ci		num_events = 256;
1268c2ecf20Sopenharmony_ci	else if (strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc/e500"))
1278c2ecf20Sopenharmony_ci		return -ENODEV;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return register_fsl_emb_pmu(&e500_pmu);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciearly_initcall(init_e500_pmu);
133