10Sduke/*-
212895Scoleenp * Copyright (c) 2003-2008 Joseph Koshy
30Sduke * Copyright (c) 2007 The FreeBSD Foundation
40Sduke * All rights reserved.
50Sduke *
60Sduke * Portions of this software were developed by A. Joseph Koshy under
70Sduke * sponsorship from the FreeBSD Foundation and Google, Inc.
80Sduke *
90Sduke * Redistribution and use in source and binary forms, with or without
100Sduke * modification, are permitted provided that the following conditions
110Sduke * are met:
120Sduke * 1. Redistributions of source code must retain the above copyright
130Sduke *    notice, this list of conditions and the following disclaimer.
140Sduke * 2. Redistributions in binary form must reproduce the above copyright
150Sduke *    notice, this list of conditions and the following disclaimer in the
160Sduke *    documentation and/or other materials provided with the distribution.
170Sduke *
180Sduke * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
191472Strims * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201472Strims * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211472Strims * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
220Sduke * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
230Sduke * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
240Sduke * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251879Sstefank * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261879Sstefank * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271879Sstefank * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281879Sstefank * SUCH DAMAGE.
291879Sstefank *
301879Sstefank * $FreeBSD$
311879Sstefank */
321879Sstefank
331879Sstefank/* Machine dependent interfaces */
341879Sstefank
351879Sstefank#ifndef _MACHINE_PMC_MDEP_H
361879Sstefank#define	_MACHINE_PMC_MDEP_H 1
371879Sstefank
381879Sstefank#ifdef	_KERNEL
391879Sstefankstruct pmc_mdep;
401879Sstefank#endif
418413Spliden
428413Spliden#include <dev/hwpmc/hwpmc_amd.h>
431879Sstefank#include <dev/hwpmc/hwpmc_core.h>
441879Sstefank#include <dev/hwpmc/hwpmc_piv.h>
459882Srprotacio#include <dev/hwpmc/hwpmc_tsc.h>
461879Sstefank#include <dev/hwpmc/hwpmc_uncore.h>
471879Sstefank
481879Sstefank/*
491879Sstefank * Intel processors implementing V2 and later of the Intel performance
501879Sstefank * measurement architecture have PMCs of the following classes: TSC,
5111857Sdholmes * IAF, IAP, UCF and UCP.
521879Sstefank */
531879Sstefank#define	PMC_MDEP_CLASS_INDEX_TSC	1
541879Sstefank#define	PMC_MDEP_CLASS_INDEX_K8		2
551879Sstefank#define	PMC_MDEP_CLASS_INDEX_P4		2
561879Sstefank#define	PMC_MDEP_CLASS_INDEX_IAP	2
571879Sstefank#define	PMC_MDEP_CLASS_INDEX_IAF	3
581879Sstefank#define	PMC_MDEP_CLASS_INDEX_UCP	4
591879Sstefank#define	PMC_MDEP_CLASS_INDEX_UCF	5
606760Sgoetz
611879Sstefank/*
621879Sstefank * On the amd64 platform we support the following PMCs.
630Sduke *
640Sduke * TSC		The timestamp counter
650Sduke * K8		AMD Athlon64 and Opteron PMCs in 64 bit mode.
660Sduke * PIV		Intel P4/HTT and P4/EMT64
670Sduke * IAP		Intel Core/Core2/Atom CPUs in 64 bits mode.
680Sduke * IAF		Intel fixed-function PMCs in Core2 and later CPUs.
690Sduke * UCP		Intel Uncore programmable PMCs.
700Sduke * UCF		Intel Uncore fixed-function PMCs.
710Sduke */
720Sduke
730Sdukeunion pmc_md_op_pmcallocate  {
740Sduke	struct pmc_md_amd_op_pmcallocate	pm_amd;
750Sduke	struct pmc_md_iaf_op_pmcallocate	pm_iaf;
760Sduke	struct pmc_md_iap_op_pmcallocate	pm_iap;
770Sduke	struct pmc_md_ucf_op_pmcallocate	pm_ucf;
780Sduke	struct pmc_md_ucp_op_pmcallocate	pm_ucp;
790Sduke	struct pmc_md_p4_op_pmcallocate		pm_p4;
800Sduke	uint64_t				__pad[4];
810Sduke};
820Sduke
830Sduke/* Logging */
840Sduke#define	PMCLOG_READADDR		PMCLOG_READ64
850Sduke#define	PMCLOG_EMITADDR		PMCLOG_EMIT64
860Sduke
870Sduke#ifdef	_KERNEL
880Sduke
890Sdukeunion pmc_md_pmc {
900Sduke	struct pmc_md_amd_pmc	pm_amd;
910Sduke	struct pmc_md_iaf_pmc	pm_iaf;
920Sduke	struct pmc_md_iap_pmc	pm_iap;
930Sduke	struct pmc_md_ucf_pmc	pm_ucf;
940Sduke	struct pmc_md_ucp_pmc	pm_ucp;
950Sduke	struct pmc_md_p4_pmc	pm_p4;
960Sduke};
970Sduke
980Sduke#define	PMC_TRAPFRAME_TO_PC(TF)	((TF)->tf_rip)
990Sduke#define	PMC_TRAPFRAME_TO_FP(TF)	((TF)->tf_rbp)
1000Sduke#define	PMC_TRAPFRAME_TO_USER_SP(TF)	((TF)->tf_rsp)
1010Sduke#define	PMC_TRAPFRAME_TO_KERNEL_SP(TF)	((TF)->tf_rsp)
1020Sduke
1030Sduke#define	PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I)		\
1040Sduke	(((I) & 0xffffffff) == 0xe5894855) /* pushq %rbp; movq %rsp,%rbp */
1050Sduke#define	PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I)		\
1060Sduke	(((I) & 0x00ffffff) == 0x00e58948) /* movq %rsp,%rbp */
1072293Sroland#define	PMC_AT_FUNCTION_EPILOGUE_RET(I)			\
1080Sduke	(((I) & 0xFF) == 0xC3)		   /* ret */
1092293Sroland
1102293Sroland#define	PMC_IN_TRAP_HANDLER(PC) 			\
1110Sduke	((PC) >= (uintptr_t) start_exceptions &&	\
1120Sduke	 (PC) < (uintptr_t) end_exceptions)
1130Sduke
1140Sduke#define	PMC_IN_KERNEL_STACK(S,START,END)		\
1150Sduke	((S) >= (START) && (S) < (END))
1160Sduke#define	PMC_IN_KERNEL(va) INKERNEL(va)
1170Sduke
1180Sduke#define	PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS)
1190Sduke
1200Sduke/* Build a fake kernel trapframe from current instruction pointer. */
1210Sduke#define PMC_FAKE_TRAPFRAME(TF)						\
1220Sduke	do {								\
1230Sduke	(TF)->tf_cs = 0; (TF)->tf_rflags = 0;				\
1240Sduke	__asm __volatile("movq %%rbp,%0" : "=r" ((TF)->tf_rbp));	\
1250Sduke	__asm __volatile("movq %%rsp,%0" : "=r" ((TF)->tf_rsp));	\
1262293Sroland	__asm __volatile("call 1f \n\t1: pop %0" : "=r"((TF)->tf_rip));	\
1276567Sthartmann	} while (0)
1286567Sthartmann
1296567Sthartmann/*
1306567Sthartmann * Prototypes
1316567Sthartmann */
1322293Sroland
1332293Srolandvoid	start_exceptions(void), end_exceptions(void);
1342293Sroland
1352293Srolandstruct pmc_mdep *pmc_amd_initialize(void);
1366567Sthartmannvoid	pmc_amd_finalize(struct pmc_mdep *_md);
1372293Srolandstruct pmc_mdep *pmc_intel_initialize(void);
1386567Sthartmannvoid	pmc_intel_finalize(struct pmc_mdep *_md);
1392293Sroland
1406567Sthartmann#endif /* _KERNEL */
1412293Sroland#endif /* _MACHINE_PMC_MDEP_H */
1426567Sthartmann