18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci#ifndef __ASM_POWERPC_IMC_PMU_H
38c2ecf20Sopenharmony_ci#define __ASM_POWERPC_IMC_PMU_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/*
68c2ecf20Sopenharmony_ci * IMC Nest Performance Monitor counter support.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2017 Madhavan Srinivasan, IBM Corporation.
98c2ecf20Sopenharmony_ci *           (C) 2017 Anju T Sudhakar, IBM Corporation.
108c2ecf20Sopenharmony_ci *           (C) 2017 Hemant K Shaw, IBM Corporation.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <linux/io.h>
178c2ecf20Sopenharmony_ci#include <asm/opal.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Compatibility macros for IMC devices
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci#define IMC_DTB_COMPAT			"ibm,opal-in-memory-counters"
238c2ecf20Sopenharmony_ci#define IMC_DTB_UNIT_COMPAT		"ibm,imc-counters"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * LDBAR: Counter address and Enable/Disable macro.
288c2ecf20Sopenharmony_ci * perf/imc-pmu.c has the LDBAR layout information.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci#define THREAD_IMC_LDBAR_MASK           0x0003ffffffffe000ULL
318c2ecf20Sopenharmony_ci#define THREAD_IMC_ENABLE               0x8000000000000000ULL
328c2ecf20Sopenharmony_ci#define TRACE_IMC_ENABLE		0x4000000000000000ULL
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * For debugfs interface for imc-mode and imc-command
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci#define IMC_CNTL_BLK_OFFSET		0x3FC00
388c2ecf20Sopenharmony_ci#define IMC_CNTL_BLK_CMD_OFFSET		8
398c2ecf20Sopenharmony_ci#define IMC_CNTL_BLK_MODE_OFFSET	32
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * Structure to hold memory address information for imc units.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_cistruct imc_mem_info {
458c2ecf20Sopenharmony_ci	u64 *vbase;
468c2ecf20Sopenharmony_ci	u32 id;
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * Place holder for nest pmu events and values.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistruct imc_events {
538c2ecf20Sopenharmony_ci	u32 value;
548c2ecf20Sopenharmony_ci	char *name;
558c2ecf20Sopenharmony_ci	char *unit;
568c2ecf20Sopenharmony_ci	char *scale;
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * Trace IMC hardware updates a 64bytes record on
618c2ecf20Sopenharmony_ci * Core Performance Monitoring Counter (CPMC)
628c2ecf20Sopenharmony_ci * overflow. Here is the layout for the trace imc record
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * DW 0 : Timebase
658c2ecf20Sopenharmony_ci * DW 1 : Program Counter
668c2ecf20Sopenharmony_ci * DW 2 : PIDR information
678c2ecf20Sopenharmony_ci * DW 3 : CPMC1
688c2ecf20Sopenharmony_ci * DW 4 : CPMC2
698c2ecf20Sopenharmony_ci * DW 5 : CPMC3
708c2ecf20Sopenharmony_ci * Dw 6 : CPMC4
718c2ecf20Sopenharmony_ci * DW 7 : Timebase
728c2ecf20Sopenharmony_ci * .....
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci * The following is the data structure to hold trace imc data.
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_cistruct trace_imc_data {
778c2ecf20Sopenharmony_ci	u64 tb1;
788c2ecf20Sopenharmony_ci	u64 ip;
798c2ecf20Sopenharmony_ci	u64 val;
808c2ecf20Sopenharmony_ci	u64 cpmc1;
818c2ecf20Sopenharmony_ci	u64 cpmc2;
828c2ecf20Sopenharmony_ci	u64 cpmc3;
838c2ecf20Sopenharmony_ci	u64 cpmc4;
848c2ecf20Sopenharmony_ci	u64 tb2;
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* Event attribute array index */
888c2ecf20Sopenharmony_ci#define IMC_FORMAT_ATTR		0
898c2ecf20Sopenharmony_ci#define IMC_EVENT_ATTR		1
908c2ecf20Sopenharmony_ci#define IMC_CPUMASK_ATTR	2
918c2ecf20Sopenharmony_ci#define IMC_NULL_ATTR		3
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* PMU Format attribute macros */
948c2ecf20Sopenharmony_ci#define IMC_EVENT_OFFSET_MASK	0xffffffffULL
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/*
978c2ecf20Sopenharmony_ci * Macro to mask bits 0:21 of first double word(which is the timebase) to
988c2ecf20Sopenharmony_ci * compare with 8th double word (timebase) of trace imc record data.
998c2ecf20Sopenharmony_ci */
1008c2ecf20Sopenharmony_ci#define IMC_TRACE_RECORD_TB1_MASK      0x3ffffffffffULL
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/*
1038c2ecf20Sopenharmony_ci * Bit 0:1 in third DW of IMC trace record
1048c2ecf20Sopenharmony_ci * specifies the MSR[HV PR] values.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_ci#define IMC_TRACE_RECORD_VAL_HVPR(x)	((x) >> 62)
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/*
1098c2ecf20Sopenharmony_ci * Device tree parser code detects IMC pmu support and
1108c2ecf20Sopenharmony_ci * registers new IMC pmus. This structure will hold the
1118c2ecf20Sopenharmony_ci * pmu functions, events, counter memory information
1128c2ecf20Sopenharmony_ci * and attrs for each imc pmu and will be referenced at
1138c2ecf20Sopenharmony_ci * the time of pmu registration.
1148c2ecf20Sopenharmony_ci */
1158c2ecf20Sopenharmony_cistruct imc_pmu {
1168c2ecf20Sopenharmony_ci	struct pmu pmu;
1178c2ecf20Sopenharmony_ci	struct imc_mem_info *mem_info;
1188c2ecf20Sopenharmony_ci	struct imc_events *events;
1198c2ecf20Sopenharmony_ci	/*
1208c2ecf20Sopenharmony_ci	 * Attribute groups for the PMU. Slot 0 used for
1218c2ecf20Sopenharmony_ci	 * format attribute, slot 1 used for cpusmask attribute,
1228c2ecf20Sopenharmony_ci	 * slot 2 used for event attribute. Slot 3 keep as
1238c2ecf20Sopenharmony_ci	 * NULL.
1248c2ecf20Sopenharmony_ci	 */
1258c2ecf20Sopenharmony_ci	const struct attribute_group *attr_groups[4];
1268c2ecf20Sopenharmony_ci	u32 counter_mem_size;
1278c2ecf20Sopenharmony_ci	int domain;
1288c2ecf20Sopenharmony_ci	/*
1298c2ecf20Sopenharmony_ci	 * flag to notify whether the memory is mmaped
1308c2ecf20Sopenharmony_ci	 * or allocated by kernel.
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci	bool imc_counter_mmaped;
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/*
1368c2ecf20Sopenharmony_ci * Structure to hold id, lock and reference count for the imc events which
1378c2ecf20Sopenharmony_ci * are inited.
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_cistruct imc_pmu_ref {
1408c2ecf20Sopenharmony_ci	spinlock_t lock;
1418c2ecf20Sopenharmony_ci	unsigned int id;
1428c2ecf20Sopenharmony_ci	int refc;
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/*
1468c2ecf20Sopenharmony_ci * In-Memory Collection Counters type.
1478c2ecf20Sopenharmony_ci * Data comes from Device tree.
1488c2ecf20Sopenharmony_ci * Three device type are supported.
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cienum {
1528c2ecf20Sopenharmony_ci	IMC_TYPE_THREAD		= 0x1,
1538c2ecf20Sopenharmony_ci	IMC_TYPE_TRACE		= 0x2,
1548c2ecf20Sopenharmony_ci	IMC_TYPE_CORE		= 0x4,
1558c2ecf20Sopenharmony_ci	IMC_TYPE_CHIP           = 0x10,
1568c2ecf20Sopenharmony_ci};
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/*
1598c2ecf20Sopenharmony_ci * Domains for IMC PMUs
1608c2ecf20Sopenharmony_ci */
1618c2ecf20Sopenharmony_ci#define IMC_DOMAIN_NEST		1
1628c2ecf20Sopenharmony_ci#define IMC_DOMAIN_CORE		2
1638c2ecf20Sopenharmony_ci#define IMC_DOMAIN_THREAD	3
1648c2ecf20Sopenharmony_ci/* For trace-imc the domain is still thread but it operates in trace-mode */
1658c2ecf20Sopenharmony_ci#define IMC_DOMAIN_TRACE	4
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciextern int init_imc_pmu(struct device_node *parent,
1688c2ecf20Sopenharmony_ci				struct imc_pmu *pmu_ptr, int pmu_id);
1698c2ecf20Sopenharmony_ciextern void thread_imc_disable(void);
1708c2ecf20Sopenharmony_ciextern int get_max_nest_dev(void);
1718c2ecf20Sopenharmony_ciextern void unregister_thread_imc(void);
1728c2ecf20Sopenharmony_ci#endif /* __ASM_POWERPC_IMC_PMU_H */
173