Path: blob/master/arch/mips/oprofile/op_model_loongson2.c
10820 views
/*1* Loongson2 performance counter driver for oprofile2*3* Copyright (C) 2009 Lemote Inc.4* Author: Yanhua <[email protected]>5* Author: Wu Zhangjin <[email protected]>6*7* This file is subject to the terms and conditions of the GNU General Public8* License. See the file "COPYING" in the main directory of this archive9* for more details.10*/11#include <linux/init.h>12#include <linux/oprofile.h>13#include <linux/interrupt.h>1415#include <loongson.h> /* LOONGSON2_PERFCNT_IRQ */16#include "op_impl.h"1718#define LOONGSON2_CPU_TYPE "mips/loongson2"1920#define LOONGSON2_PERFCNT_OVERFLOW (1ULL << 31)2122#define LOONGSON2_PERFCTRL_EXL (1UL << 0)23#define LOONGSON2_PERFCTRL_KERNEL (1UL << 1)24#define LOONGSON2_PERFCTRL_SUPERVISOR (1UL << 2)25#define LOONGSON2_PERFCTRL_USER (1UL << 3)26#define LOONGSON2_PERFCTRL_ENABLE (1UL << 4)27#define LOONGSON2_PERFCTRL_EVENT(idx, event) \28(((event) & 0x0f) << ((idx) ? 9 : 5))2930#define read_c0_perfctrl() __read_64bit_c0_register($24, 0)31#define write_c0_perfctrl(val) __write_64bit_c0_register($24, 0, val)32#define read_c0_perfcnt() __read_64bit_c0_register($25, 0)33#define write_c0_perfcnt(val) __write_64bit_c0_register($25, 0, val)3435static struct loongson2_register_config {36unsigned int ctrl;37unsigned long long reset_counter1;38unsigned long long reset_counter2;39int cnt1_enabled, cnt2_enabled;40} reg;4142static char *oprofid = "LoongsonPerf";43static irqreturn_t loongson2_perfcount_handler(int irq, void *dev_id);4445static void reset_counters(void *arg)46{47write_c0_perfctrl(0);48write_c0_perfcnt(0);49}5051static void loongson2_reg_setup(struct op_counter_config *cfg)52{53unsigned int ctrl = 0;5455reg.reset_counter1 = 0;56reg.reset_counter2 = 0;5758/*59* Compute the performance counter ctrl word.60* For now, count kernel and user mode.61*/62if (cfg[0].enabled) {63ctrl |= LOONGSON2_PERFCTRL_EVENT(0, cfg[0].event);64reg.reset_counter1 = 0x80000000ULL - cfg[0].count;65}6667if (cfg[1].enabled) {68ctrl |= LOONGSON2_PERFCTRL_EVENT(1, cfg[1].event);69reg.reset_counter2 = 0x80000000ULL - cfg[1].count;70}7172if (cfg[0].enabled || cfg[1].enabled) {73ctrl |= LOONGSON2_PERFCTRL_EXL | LOONGSON2_PERFCTRL_ENABLE;74if (cfg[0].kernel || cfg[1].kernel)75ctrl |= LOONGSON2_PERFCTRL_KERNEL;76if (cfg[0].user || cfg[1].user)77ctrl |= LOONGSON2_PERFCTRL_USER;78}7980reg.ctrl = ctrl;8182reg.cnt1_enabled = cfg[0].enabled;83reg.cnt2_enabled = cfg[1].enabled;84}8586static void loongson2_cpu_setup(void *args)87{88write_c0_perfcnt((reg.reset_counter2 << 32) | reg.reset_counter1);89}9091static void loongson2_cpu_start(void *args)92{93/* Start all counters on current CPU */94if (reg.cnt1_enabled || reg.cnt2_enabled)95write_c0_perfctrl(reg.ctrl);96}9798static void loongson2_cpu_stop(void *args)99{100/* Stop all counters on current CPU */101write_c0_perfctrl(0);102memset(®, 0, sizeof(reg));103}104105static irqreturn_t loongson2_perfcount_handler(int irq, void *dev_id)106{107uint64_t counter, counter1, counter2;108struct pt_regs *regs = get_irq_regs();109int enabled;110111/* Check whether the irq belongs to me */112enabled = read_c0_perfctrl() & LOONGSON2_PERFCTRL_ENABLE;113if (!enabled)114return IRQ_NONE;115enabled = reg.cnt1_enabled | reg.cnt2_enabled;116if (!enabled)117return IRQ_NONE;118119counter = read_c0_perfcnt();120counter1 = counter & 0xffffffff;121counter2 = counter >> 32;122123if (counter1 & LOONGSON2_PERFCNT_OVERFLOW) {124if (reg.cnt1_enabled)125oprofile_add_sample(regs, 0);126counter1 = reg.reset_counter1;127}128if (counter2 & LOONGSON2_PERFCNT_OVERFLOW) {129if (reg.cnt2_enabled)130oprofile_add_sample(regs, 1);131counter2 = reg.reset_counter2;132}133134write_c0_perfcnt((counter2 << 32) | counter1);135136return IRQ_HANDLED;137}138139static int __init loongson2_init(void)140{141return request_irq(LOONGSON2_PERFCNT_IRQ, loongson2_perfcount_handler,142IRQF_SHARED, "Perfcounter", oprofid);143}144145static void loongson2_exit(void)146{147reset_counters(NULL);148free_irq(LOONGSON2_PERFCNT_IRQ, oprofid);149}150151struct op_mips_model op_model_loongson2_ops = {152.reg_setup = loongson2_reg_setup,153.cpu_setup = loongson2_cpu_setup,154.init = loongson2_init,155.exit = loongson2_exit,156.cpu_start = loongson2_cpu_start,157.cpu_stop = loongson2_cpu_stop,158.cpu_type = LOONGSON2_CPU_TYPE,159.num_counters = 2160};161162163