Path: blob/master/arch/sh/kernel/cpu/sh4/perf_event.c
37217 views
/*1* Performance events support for SH7750-style performance counters2*3* Copyright (C) 2009 Paul Mundt4*5* This file is subject to the terms and conditions of the GNU General Public6* License. See the file "COPYING" in the main directory of this archive7* for more details.8*/9#include <linux/kernel.h>10#include <linux/init.h>11#include <linux/io.h>12#include <linux/irq.h>13#include <linux/perf_event.h>14#include <asm/processor.h>1516#define PM_CR_BASE 0xff000084 /* 16-bit */17#define PM_CTR_BASE 0xff100004 /* 32-bit */1819#define PMCR(n) (PM_CR_BASE + ((n) * 0x04))20#define PMCTRH(n) (PM_CTR_BASE + 0x00 + ((n) * 0x08))21#define PMCTRL(n) (PM_CTR_BASE + 0x04 + ((n) * 0x08))2223#define PMCR_PMM_MASK 0x0000003f2425#define PMCR_CLKF 0x0000010026#define PMCR_PMCLR 0x0000200027#define PMCR_PMST 0x0000400028#define PMCR_PMEN 0x000080002930static struct sh_pmu sh7750_pmu;3132/*33* There are a number of events supported by each counter (33 in total).34* Since we have 2 counters, each counter will take the event code as it35* corresponds to the PMCR PMM setting. Each counter can be configured36* independently.37*38* Event Code Description39* ---------- -----------40*41* 0x01 Operand read access42* 0x02 Operand write access43* 0x03 UTLB miss44* 0x04 Operand cache read miss45* 0x05 Operand cache write miss46* 0x06 Instruction fetch (w/ cache)47* 0x07 Instruction TLB miss48* 0x08 Instruction cache miss49* 0x09 All operand accesses50* 0x0a All instruction accesses51* 0x0b OC RAM operand access52* 0x0d On-chip I/O space access53* 0x0e Operand access (r/w)54* 0x0f Operand cache miss (r/w)55* 0x10 Branch instruction56* 0x11 Branch taken57* 0x12 BSR/BSRF/JSR58* 0x13 Instruction execution59* 0x14 Instruction execution in parallel60* 0x15 FPU Instruction execution61* 0x16 Interrupt62* 0x17 NMI63* 0x18 trapa instruction execution64* 0x19 UBCA match65* 0x1a UBCB match66* 0x21 Instruction cache fill67* 0x22 Operand cache fill68* 0x23 Elapsed time69* 0x24 Pipeline freeze by I-cache miss70* 0x25 Pipeline freeze by D-cache miss71* 0x27 Pipeline freeze by branch instruction72* 0x28 Pipeline freeze by CPU register73* 0x29 Pipeline freeze by FPU74*/7576static const int sh7750_general_events[] = {77[PERF_COUNT_HW_CPU_CYCLES] = 0x0023,78[PERF_COUNT_HW_INSTRUCTIONS] = 0x000a,79[PERF_COUNT_HW_CACHE_REFERENCES] = 0x0006, /* I-cache */80[PERF_COUNT_HW_CACHE_MISSES] = 0x0008, /* I-cache */81[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x0010,82[PERF_COUNT_HW_BRANCH_MISSES] = -1,83[PERF_COUNT_HW_BUS_CYCLES] = -1,84};8586#define C(x) PERF_COUNT_HW_CACHE_##x8788static const int sh7750_cache_events89[PERF_COUNT_HW_CACHE_MAX]90[PERF_COUNT_HW_CACHE_OP_MAX]91[PERF_COUNT_HW_CACHE_RESULT_MAX] =92{93[ C(L1D) ] = {94[ C(OP_READ) ] = {95[ C(RESULT_ACCESS) ] = 0x0001,96[ C(RESULT_MISS) ] = 0x0004,97},98[ C(OP_WRITE) ] = {99[ C(RESULT_ACCESS) ] = 0x0002,100[ C(RESULT_MISS) ] = 0x0005,101},102[ C(OP_PREFETCH) ] = {103[ C(RESULT_ACCESS) ] = 0,104[ C(RESULT_MISS) ] = 0,105},106},107108[ C(L1I) ] = {109[ C(OP_READ) ] = {110[ C(RESULT_ACCESS) ] = 0x0006,111[ C(RESULT_MISS) ] = 0x0008,112},113[ C(OP_WRITE) ] = {114[ C(RESULT_ACCESS) ] = -1,115[ C(RESULT_MISS) ] = -1,116},117[ C(OP_PREFETCH) ] = {118[ C(RESULT_ACCESS) ] = 0,119[ C(RESULT_MISS) ] = 0,120},121},122123[ C(LL) ] = {124[ C(OP_READ) ] = {125[ C(RESULT_ACCESS) ] = 0,126[ C(RESULT_MISS) ] = 0,127},128[ C(OP_WRITE) ] = {129[ C(RESULT_ACCESS) ] = 0,130[ C(RESULT_MISS) ] = 0,131},132[ C(OP_PREFETCH) ] = {133[ C(RESULT_ACCESS) ] = 0,134[ C(RESULT_MISS) ] = 0,135},136},137138[ C(DTLB) ] = {139[ C(OP_READ) ] = {140[ C(RESULT_ACCESS) ] = 0,141[ C(RESULT_MISS) ] = 0x0003,142},143[ C(OP_WRITE) ] = {144[ C(RESULT_ACCESS) ] = 0,145[ C(RESULT_MISS) ] = 0,146},147[ C(OP_PREFETCH) ] = {148[ C(RESULT_ACCESS) ] = 0,149[ C(RESULT_MISS) ] = 0,150},151},152153[ C(ITLB) ] = {154[ C(OP_READ) ] = {155[ C(RESULT_ACCESS) ] = 0,156[ C(RESULT_MISS) ] = 0x0007,157},158[ C(OP_WRITE) ] = {159[ C(RESULT_ACCESS) ] = -1,160[ C(RESULT_MISS) ] = -1,161},162[ C(OP_PREFETCH) ] = {163[ C(RESULT_ACCESS) ] = -1,164[ C(RESULT_MISS) ] = -1,165},166},167168[ C(BPU) ] = {169[ C(OP_READ) ] = {170[ C(RESULT_ACCESS) ] = -1,171[ C(RESULT_MISS) ] = -1,172},173[ C(OP_WRITE) ] = {174[ C(RESULT_ACCESS) ] = -1,175[ C(RESULT_MISS) ] = -1,176},177[ C(OP_PREFETCH) ] = {178[ C(RESULT_ACCESS) ] = -1,179[ C(RESULT_MISS) ] = -1,180},181},182};183184static int sh7750_event_map(int event)185{186return sh7750_general_events[event];187}188189static u64 sh7750_pmu_read(int idx)190{191return (u64)((u64)(__raw_readl(PMCTRH(idx)) & 0xffff) << 32) |192__raw_readl(PMCTRL(idx));193}194195static void sh7750_pmu_disable(struct hw_perf_event *hwc, int idx)196{197unsigned int tmp;198199tmp = __raw_readw(PMCR(idx));200tmp &= ~(PMCR_PMM_MASK | PMCR_PMEN);201__raw_writew(tmp, PMCR(idx));202}203204static void sh7750_pmu_enable(struct hw_perf_event *hwc, int idx)205{206__raw_writew(__raw_readw(PMCR(idx)) | PMCR_PMCLR, PMCR(idx));207__raw_writew(hwc->config | PMCR_PMEN | PMCR_PMST, PMCR(idx));208}209210static void sh7750_pmu_disable_all(void)211{212int i;213214for (i = 0; i < sh7750_pmu.num_events; i++)215__raw_writew(__raw_readw(PMCR(i)) & ~PMCR_PMEN, PMCR(i));216}217218static void sh7750_pmu_enable_all(void)219{220int i;221222for (i = 0; i < sh7750_pmu.num_events; i++)223__raw_writew(__raw_readw(PMCR(i)) | PMCR_PMEN, PMCR(i));224}225226static struct sh_pmu sh7750_pmu = {227.name = "sh7750",228.num_events = 2,229.event_map = sh7750_event_map,230.max_events = ARRAY_SIZE(sh7750_general_events),231.raw_event_mask = PMCR_PMM_MASK,232.cache_events = &sh7750_cache_events,233.read = sh7750_pmu_read,234.disable = sh7750_pmu_disable,235.enable = sh7750_pmu_enable,236.disable_all = sh7750_pmu_disable_all,237.enable_all = sh7750_pmu_enable_all,238};239240static int __init sh7750_pmu_init(void)241{242/*243* Make sure this CPU actually has perf counters.244*/245if (!(boot_cpu_data.flags & CPU_HAS_PERF_COUNTER)) {246pr_notice("HW perf events unsupported, software events only.\n");247return -ENODEV;248}249250return register_sh_pmu(&sh7750_pmu);251}252early_initcall(sh7750_pmu_init);253254255