Blame src/libpfm4/lib/pfmlib_intel_x86_arch.c

Packit Service a1973e
/*
Packit Service a1973e
 * pfmlib_intel_x86_arch.c : Intel architectural PMU v1, v2, v3
Packit Service a1973e
 *
Packit Service a1973e
 * Copyright (c) 2005-2007 Hewlett-Packard Development Company, L.P.
Packit Service a1973e
 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
Packit Service a1973e
 *
Packit Service a1973e
 *
Packit Service a1973e
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit Service a1973e
 * of this software and associated documentation files (the "Software"), to deal
Packit Service a1973e
 * in the Software without restriction, including without limitation the rights
Packit Service a1973e
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
Packit Service a1973e
 * of the Software, and to permit persons to whom the Software is furnished to do so,
Packit Service a1973e
 * subject to the following conditions:
Packit Service a1973e
 *
Packit Service a1973e
 * The above copyright notice and this permission notice shall be included in all
Packit Service a1973e
 * copies or substantial portions of the Software.
Packit Service a1973e
 *
Packit Service a1973e
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
Packit Service a1973e
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
Packit Service a1973e
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit Service a1973e
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Packit Service a1973e
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
Packit Service a1973e
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit Service a1973e
 *
Packit Service a1973e
 *
Packit Service a1973e
 * This file implements supports for the IA-32 architectural PMU as specified
Packit Service a1973e
 * in the following document:
Packit Service a1973e
 * 	"IA-32 Intel Architecture Software Developer's Manual - Volume 3B: System
Packit Service a1973e
 * 	Programming Guide"
Packit Service a1973e
 */
Packit Service a1973e
#include <sys/types.h>
Packit Service a1973e
#include <ctype.h>
Packit Service a1973e
#include <string.h>
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
Packit Service a1973e
/* private headers */
Packit Service a1973e
#include "pfmlib_priv.h"			/* library private */
Packit Service a1973e
#include "pfmlib_intel_x86_priv.h"		/* architecture private */
Packit Service a1973e
Packit Service a1973e
#include "events/intel_x86_arch_events.h"	/* architected event table */
Packit Service a1973e
Packit Service a1973e
extern pfmlib_pmu_t intel_x86_arch_support;
Packit Service a1973e
Packit Service a1973e
static intel_x86_entry_t *x86_arch_pe;
Packit Service a1973e
/*
Packit Service a1973e
 * .byte 0x53 == push ebx. it's universal for 32 and 64 bit
Packit Service a1973e
 * .byte 0x5b == pop ebx.
Packit Service a1973e
 * Some gcc's (4.1.2 on Core2) object to pairing push/pop and ebx in 64 bit mode.
Packit Service a1973e
 * Using the opcode directly avoids this problem.
Packit Service a1973e
 */
Packit Service a1973e
static inline void
Packit Service a1973e
cpuid(unsigned int op, unsigned int *a, unsigned int *b, unsigned int *c, unsigned int *d)
Packit Service a1973e
{
Packit Service a1973e
  __asm__ __volatile__ (".byte 0x53\n\tcpuid\n\tmovl %%ebx, %%esi\n\t.byte 0x5b"
Packit Service a1973e
       : "=a" (*a),
Packit Service a1973e
	     "=S" (*b),
Packit Service a1973e
		 "=c" (*c),
Packit Service a1973e
		 "=d" (*d)
Packit Service a1973e
       : "a" (op));
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
/*
Packit Service a1973e
 * create architected event table
Packit Service a1973e
 */
Packit Service a1973e
static int
Packit Service a1973e
create_arch_event_table(unsigned int mask, int version)
Packit Service a1973e
{
Packit Service a1973e
	intel_x86_entry_t *pe;
Packit Service a1973e
	int i, num_events = 0;
Packit Service a1973e
	int m;
Packit Service a1973e
Packit Service a1973e
	DPRINT("version=%d evt_msk=0x%x\n", version, mask);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * first pass: count the number of supported events
Packit Service a1973e
	 */
Packit Service a1973e
	m = mask;
Packit Service a1973e
	for(i=0; i < 7; i++, m>>=1) {
Packit Service a1973e
		if ((m & 0x1)  == 0)
Packit Service a1973e
			num_events++;
Packit Service a1973e
	}
Packit Service a1973e
	intel_x86_arch_support.pme_count = num_events;
Packit Service a1973e
Packit Service a1973e
	pe = calloc(num_events, sizeof(intel_x86_entry_t));
Packit Service a1973e
	if (pe == NULL)
Packit Service a1973e
		return PFM_ERR_NOTSUPP;
Packit Service a1973e
Packit Service a1973e
	x86_arch_pe = pe;
Packit Service a1973e
	intel_x86_arch_support.pe = pe;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * second pass: populate the table
Packit Service a1973e
	 */
Packit Service a1973e
	m = mask;
Packit Service a1973e
	for(i=0; i < 7; i++, m>>=1) {
Packit Service a1973e
		if (!(m & 0x1)) {
Packit Service a1973e
			*pe = intel_x86_arch_pe[i];
Packit Service a1973e
Packit Service a1973e
			switch(version) {
Packit Service a1973e
			case 3:
Packit Service a1973e
				pe->modmsk = INTEL_V3_ATTRS;
Packit Service a1973e
				break;
Packit Service a1973e
			default:
Packit Service a1973e
				pe->modmsk = INTEL_V2_ATTRS;
Packit Service a1973e
				break;
Packit Service a1973e
			}
Packit Service a1973e
			pe++;
Packit Service a1973e
		}
Packit Service a1973e
	}
Packit Service a1973e
	return PFM_SUCCESS;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static int
Packit Service a1973e
check_arch_pmu(int family)
Packit Service a1973e
{
Packit Service a1973e
	union {
Packit Service a1973e
		unsigned int val;
Packit Service a1973e
		intel_x86_pmu_eax_t eax;
Packit Service a1973e
		intel_x86_pmu_edx_t edx;
Packit Service a1973e
	} eax, ecx, edx, ebx;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * check family number to reject for processors
Packit Service a1973e
	 * older than Pentium (family=5). Those processors
Packit Service a1973e
	 * did not have the CPUID instruction
Packit Service a1973e
	 */
Packit Service a1973e
	if (family < 5 || family == 15)
Packit Service a1973e
		return PFM_ERR_NOTSUPP;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * check if CPU supports 0xa function of CPUID
Packit Service a1973e
	 * 0xa started with Core Duo. Needed to detect if
Packit Service a1973e
	 * architected PMU is present
Packit Service a1973e
	 */
Packit Service a1973e
	cpuid(0x0, &eax.val, &ebx.val, &ecx.val, &edx.val);
Packit Service a1973e
	if (eax.val < 0xa)
Packit Service a1973e
		return PFM_ERR_NOTSUPP;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * extract architected PMU information
Packit Service a1973e
	 */
Packit Service a1973e
	cpuid(0xa, &eax.val, &ebx.val, &ecx.val, &edx.val);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * version must be greater than zero
Packit Service a1973e
	 */
Packit Service a1973e
	return eax.eax.version < 1 ? PFM_ERR_NOTSUPP : PFM_SUCCESS;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static int
Packit Service a1973e
pfm_intel_x86_arch_detect(void *this)
Packit Service a1973e
{
Packit Service a1973e
	int ret;
Packit Service a1973e
Packit Service a1973e
	ret = pfm_intel_x86_detect();
Packit Service a1973e
	if (ret != PFM_SUCCESS)
Packit Service a1973e
		return ret;
Packit Service a1973e
Packit Service a1973e
	return check_arch_pmu(pfm_intel_x86_cfg.family);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static int
Packit Service a1973e
pfm_intel_x86_arch_init(void *this)
Packit Service a1973e
{
Packit Service a1973e
	union {
Packit Service a1973e
		unsigned int val;
Packit Service a1973e
		intel_x86_pmu_eax_t eax;
Packit Service a1973e
		intel_x86_pmu_edx_t edx;
Packit Service a1973e
	} eax, ecx, edx, ebx;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * extract architected PMU information
Packit Service a1973e
	 */
Packit Service a1973e
	if (!pfm_cfg.forced_pmu) {
Packit Service a1973e
		cpuid(0xa, &eax.val, &ebx.val, &ecx.val, &edx.val);
Packit Service a1973e
		intel_x86_arch_support.num_cntrs = eax.eax.num_cnt;
Packit Service a1973e
		intel_x86_arch_support.num_fixed_cntrs = edx.edx.num_cnt;
Packit Service a1973e
	} else {
Packit Service a1973e
		eax.eax.version = 3;
Packit Service a1973e
		ebx.val = 0; /* no restriction */
Packit Service a1973e
		intel_x86_arch_support.num_cntrs = 0;
Packit Service a1973e
		intel_x86_arch_support.num_fixed_cntrs = 0;
Packit Service a1973e
	}
Packit Service a1973e
	/*
Packit Service a1973e
 	 * must be called after impl_cntrs has been initialized
Packit Service a1973e
 	 */
Packit Service a1973e
	return create_arch_event_table(ebx.val, eax.eax.version);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
void
Packit Service a1973e
pfm_intel_x86_arch_terminate(void *this)
Packit Service a1973e
{
Packit Service a1973e
	/* workaround const void for intel_x86_arch_support.pe */
Packit Service a1973e
	if (x86_arch_pe)
Packit Service a1973e
		free(x86_arch_pe);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
/* architected PMU */
Packit Service a1973e
pfmlib_pmu_t intel_x86_arch_support={
Packit Service a1973e
	.desc			= "Intel X86 architectural PMU",
Packit Service a1973e
	.name			= "ix86arch",
Packit Service a1973e
	.pmu			= PFM_PMU_INTEL_X86_ARCH,
Packit Service a1973e
	.pme_count		= 0,
Packit Service a1973e
	.pe			= NULL,
Packit Service a1973e
	.atdesc			= intel_x86_mods,
Packit Service a1973e
	.flags			= PFMLIB_PMU_FL_RAW_UMASK | PFMLIB_PMU_FL_ARCH_DFL,
Packit Service a1973e
	.type			= PFM_PMU_TYPE_CORE,
Packit Service a1973e
	.max_encoding		= 1,
Packit Service a1973e
Packit Service a1973e
	.pmu_detect		= pfm_intel_x86_arch_detect,
Packit Service a1973e
	.pmu_init		= pfm_intel_x86_arch_init,
Packit Service a1973e
	.pmu_terminate		= pfm_intel_x86_arch_terminate,
Packit Service a1973e
	.get_event_encoding[PFM_OS_NONE] = pfm_intel_x86_get_encoding,
Packit Service a1973e
	 PFMLIB_ENCODE_PERF(pfm_intel_x86_get_perf_encoding),
Packit Service a1973e
	.get_event_first	= pfm_intel_x86_get_event_first,
Packit Service a1973e
	.get_event_next		= pfm_intel_x86_get_event_next,
Packit Service a1973e
	.event_is_valid		= pfm_intel_x86_event_is_valid,
Packit Service a1973e
	.get_event_info		= pfm_intel_x86_get_event_info,
Packit Service a1973e
	.get_event_attr_info	= pfm_intel_x86_get_event_attr_info,
Packit Service a1973e
	PFMLIB_VALID_PERF_PATTRS(pfm_intel_x86_perf_validate_pattrs),
Packit Service a1973e
	.get_event_nattrs	= pfm_intel_x86_get_event_nattrs,
Packit Service a1973e
};