18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Performance counter support for e6500 family processors.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Priyanka Jain, Priyanka.Jain@freescale.com
68c2ecf20Sopenharmony_ci * Based on e500-pmu.c
78c2ecf20Sopenharmony_ci * Copyright 2013 Freescale Semiconductor, Inc.
88c2ecf20Sopenharmony_ci * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/string.h>
128c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
138c2ecf20Sopenharmony_ci#include <asm/reg.h>
148c2ecf20Sopenharmony_ci#include <asm/cputable.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * Map of generic hardware event types to hardware events
188c2ecf20Sopenharmony_ci * Zero if unsupported
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_cistatic int e6500_generic_events[] = {
218c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CPU_CYCLES] = 1,
228c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_INSTRUCTIONS] = 2,
238c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_MISSES] = 221,
248c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 12,
258c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_MISSES] = 15,
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define C(x)	PERF_COUNT_HW_CACHE_##x
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Table of generalized cache-related events.
328c2ecf20Sopenharmony_ci * 0 means not supported, -1 means nonsensical, other values
338c2ecf20Sopenharmony_ci * are event codes.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_cistatic int e6500_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
368c2ecf20Sopenharmony_ci	[C(L1D)] = {
378c2ecf20Sopenharmony_ci				/*RESULT_ACCESS		RESULT_MISS */
388c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	27,		222	},
398c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	28,		223	},
408c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	29,		0	},
418c2ecf20Sopenharmony_ci	},
428c2ecf20Sopenharmony_ci	[C(L1I)] = {
438c2ecf20Sopenharmony_ci				/*RESULT_ACCESS		RESULT_MISS */
448c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	2,		254	},
458c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
468c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	37,		0	},
478c2ecf20Sopenharmony_ci	},
488c2ecf20Sopenharmony_ci	/*
498c2ecf20Sopenharmony_ci	 * Assuming LL means L2, it's not a good match for this model.
508c2ecf20Sopenharmony_ci	 * It does not have separate read/write events (but it does have
518c2ecf20Sopenharmony_ci	 * separate instruction/data events).
528c2ecf20Sopenharmony_ci	 */
538c2ecf20Sopenharmony_ci	[C(LL)] = {
548c2ecf20Sopenharmony_ci				/*RESULT_ACCESS		RESULT_MISS */
558c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0	},
568c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0,		0	},
578c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0,		0	},
588c2ecf20Sopenharmony_ci	},
598c2ecf20Sopenharmony_ci	/*
608c2ecf20Sopenharmony_ci	 * There are data/instruction MMU misses, but that's a miss on
618c2ecf20Sopenharmony_ci	 * the chip's internal level-one TLB which is probably not
628c2ecf20Sopenharmony_ci	 * what the user wants.  Instead, unified level-two TLB misses
638c2ecf20Sopenharmony_ci	 * are reported here.
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	[C(DTLB)] = {
668c2ecf20Sopenharmony_ci				/*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)] = {
728c2ecf20Sopenharmony_ci				/*RESULT_ACCESS		RESULT_MISS */
738c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	12,		15	},
748c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
758c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
768c2ecf20Sopenharmony_ci	},
778c2ecf20Sopenharmony_ci	[C(NODE)] = {
788c2ecf20Sopenharmony_ci				/* RESULT_ACCESS	RESULT_MISS */
798c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	-1,		-1	},
808c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
818c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
828c2ecf20Sopenharmony_ci	},
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic int num_events = 512;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* Upper half of event id is PMLCb, for threshold events */
888c2ecf20Sopenharmony_cistatic u64 e6500_xlate_event(u64 event_id)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	u32 event_low = (u32)event_id;
918c2ecf20Sopenharmony_ci	if (event_low >= num_events ||
928c2ecf20Sopenharmony_ci		(event_id & (FSL_EMB_EVENT_THRESHMUL | FSL_EMB_EVENT_THRESH)))
938c2ecf20Sopenharmony_ci		return 0;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return FSL_EMB_EVENT_VALID;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic struct fsl_emb_pmu e6500_pmu = {
998c2ecf20Sopenharmony_ci	.name			= "e6500 family",
1008c2ecf20Sopenharmony_ci	.n_counter		= 6,
1018c2ecf20Sopenharmony_ci	.n_restricted		= 0,
1028c2ecf20Sopenharmony_ci	.xlate_event		= e6500_xlate_event,
1038c2ecf20Sopenharmony_ci	.n_generic		= ARRAY_SIZE(e6500_generic_events),
1048c2ecf20Sopenharmony_ci	.generic_events		= e6500_generic_events,
1058c2ecf20Sopenharmony_ci	.cache_events		= &e6500_cache_events,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int init_e6500_pmu(void)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	if (!cur_cpu_spec->oprofile_cpu_type ||
1118c2ecf20Sopenharmony_ci		strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc/e6500"))
1128c2ecf20Sopenharmony_ci		return -ENODEV;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return register_fsl_emb_pmu(&e6500_pmu);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciearly_initcall(init_e6500_pmu);
118