From patchwork Thu Oct 16 13:52:03 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yogesh Tillu X-Patchwork-Id: 38816 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f198.google.com (mail-lb0-f198.google.com [209.85.217.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id ABC3820973 for ; Thu, 16 Oct 2014 13:56:13 +0000 (UTC) Received: by mail-lb0-f198.google.com with SMTP id 10sf1951124lbg.1 for ; Thu, 16 Oct 2014 06:56:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:cc:subject:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list:content-type :content-transfer-encoding; bh=SK0BsoHzPX1Ajk+Srzwx6x05bh0dEqK3fkNJ0R4Bo1E=; b=jlEnNtZLqVcX3lCBqwwkitgTvyvsvO/Upq6wYe/DimNTyo907xhL38upwC3uD+nIPM L5hQmFVBpL/L3bAPMo6Wq59orhmNH/AU2qlMARM/wDwyXXduD1VgQJPMYa6fyfaN3YFj H1amPcpKFRG/DspJtaMlENmVlfw2RDJsSmNqq+iFi3YBd3xHf+DMx+udGB6bbtSKfkbC rabq+ydHfUdhRJJeOLEOgsbZoV89CGqnWf/M1l4KsppoUoCRLSu5Iuur/vevtWCSUUiG Y8p4zRDFnjeDLx4lFxuryYE8KtR7/MBFECyLnW0GyscoOzdH1C/3T1KlAhDnzbGqoHsz ofXQ== X-Gm-Message-State: ALoCoQlvcMO9nCPbj4wmSfAvlAKFfAATGwHbyIayBXEpX3MLJr30zVfNtNri6eI8VJagQUPdJ04G X-Received: by 10.112.63.103 with SMTP id f7mr303339lbs.9.1413467772425; Thu, 16 Oct 2014 06:56:12 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.25.165 with SMTP id d5ls162842lag.1.gmail; Thu, 16 Oct 2014 06:56:12 -0700 (PDT) X-Received: by 10.112.144.228 with SMTP id sp4mr1845305lbb.58.1413467772271; Thu, 16 Oct 2014 06:56:12 -0700 (PDT) Received: from mail-la0-f47.google.com (mail-la0-f47.google.com [209.85.215.47]) by mx.google.com with ESMTPS id xx3si34813731lbb.122.2014.10.16.06.56.12 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 16 Oct 2014 06:56:12 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.47 as permitted sender) client-ip=209.85.215.47; Received: by mail-la0-f47.google.com with SMTP id pv20so2889858lab.6 for ; Thu, 16 Oct 2014 06:56:12 -0700 (PDT) X-Received: by 10.152.6.228 with SMTP id e4mr1741344laa.71.1413467772129; Thu, 16 Oct 2014 06:56:12 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.84.229 with SMTP id c5csp43482lbz; Thu, 16 Oct 2014 06:56:11 -0700 (PDT) X-Received: by 10.220.202.72 with SMTP id fd8mr872587vcb.59.1413467770696; Thu, 16 Oct 2014 06:56:10 -0700 (PDT) Received: from ip-10-35-177-41.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id ka13si20176043vdb.22.2014.10.16.06.56.09 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 16 Oct 2014 06:56:10 -0700 (PDT) Received-SPF: none (google.com: lng-odp-bounces@lists.linaro.org does not designate permitted sender hosts) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-35-177-41.ec2.internal) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XelXC-0008Mq-ET; Thu, 16 Oct 2014 13:56:06 +0000 Received: from mail-pd0-f171.google.com ([209.85.192.171]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1XelX3-0008MM-NE for lng-odp@lists.linaro.org; Thu, 16 Oct 2014 13:55:57 +0000 Received: by mail-pd0-f171.google.com with SMTP id ft15so3300562pdb.2 for ; Thu, 16 Oct 2014 06:55:52 -0700 (PDT) X-Received: by 10.67.5.41 with SMTP id cj9mr1438722pad.73.1413467751994; Thu, 16 Oct 2014 06:55:51 -0700 (PDT) Received: from yogesh-Dell-System-Vostro-3360.Airtel4Grouter.cpe ([14.140.2.178]) by mx.google.com with ESMTPSA id 16sm19899221pdj.42.2014.10.16.06.55.49 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 16 Oct 2014 06:55:51 -0700 (PDT) From: Yogesh Tillu To: lng-odp@lists.linaro.org Date: Thu, 16 Oct 2014 19:22:03 +0530 Message-Id: <1413467525-30304-2-git-send-email-yogesh.tillu@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1413467525-30304-1-git-send-email-yogesh.tillu@linaro.org> References: <1413467525-30304-1-git-send-email-yogesh.tillu@linaro.org> X-Topics: patch Cc: Yogesh Tillu Subject: [lng-odp] [PATCH] Kernel module to Enable userspace access to PMU counters for ArmV8 X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: lng-odp-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: yogesh.tillu@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.47 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Signed-off-by: Yogesh Tillu --- ARMv8_Module/Makefile | 8 ++++ ARMv8_Module/README | 1 + ARMv8_Module/enable_arm_pmu.c | 96 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 ARMv8_Module/Makefile create mode 100644 ARMv8_Module/README create mode 100644 ARMv8_Module/enable_arm_pmu.c diff --git a/ARMv8_Module/Makefile b/ARMv8_Module/Makefile new file mode 100644 index 0000000..19a31ea --- /dev/null +++ b/ARMv8_Module/Makefile @@ -0,0 +1,8 @@ +obj-m := enable_arm_pmu.o +KDIR := /lib/modules/$(shell uname -r)/build +PWD := $(shell pwd) + +all: + $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules +clean: + $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean diff --git a/ARMv8_Module/README b/ARMv8_Module/README new file mode 100644 index 0000000..648456b --- /dev/null +++ b/ARMv8_Module/README @@ -0,0 +1 @@ +make ARCH=arm64 clean;make ARCH=arm64 CROSS_COMPILE=~/arm64-tc-14.06/bin/aarch64-linux-gnu- -C ~/work/lava_ci/juno/linux-linaro/workspace/builddir-3.16.0-linaro-juno/ SUBDIRS=`pwd` diff --git a/ARMv8_Module/enable_arm_pmu.c b/ARMv8_Module/enable_arm_pmu.c new file mode 100644 index 0000000..5c87b08 --- /dev/null +++ b/ARMv8_Module/enable_arm_pmu.c @@ -0,0 +1,96 @@ +/* + * Enable user-mode ARM performance counter access. + */ +#include +#include +#include +/** -- Configuration stuff ------------------------------------------------- */ + +#define DRVR_NAME "enable_arm_pmu" + +#if !defined(__aarch64__) + #error Module can only be compiled on ARM 64 machines. +#endif + +/** -- Initialization & boilerplate ---------------------------------------- */ + +#define PERF_DEF_OPTS (1 | 16) +#define PERF_OPT_RESET_CYCLES (2 | 4) +#define PERF_OPT_DIV64 (8) +#define ARMV8_PMCR_MASK 0x3f +#define ARMV8_PMCR_E (1 << 0) /* Enable all counters */ +#define ARMV8_PMCR_P (1 << 1) /* Reset all counters */ +#define ARMV8_PMCR_C (1 << 2) /* Cycle counter reset */ +#define ARMV8_PMCR_D (1 << 3) /* CCNT counts every 64th cpu cycle */ +#define ARMV8_PMCR_X (1 << 4) /* Export to ETM */ +#define ARMV8_PMCR_DP (1 << 5) /* Disable CCNT if non-invasive debug*/ +#define ARMV8_PMCR_N_SHIFT 11 /* Number of counters supported */ +#define ARMV8_PMCR_N_MASK 0x1f + +#define ARMV8_PMUSERENR_EN_EL0 (1 << 0) /* EL0 access enable */ +#define ARMV8_PMUSERENR_CR (1 << 2) /* Cycle counter read enable */ +#define ARMV8_PMUSERENR_ER (1 << 3) /* Event counter read enable */ + +static inline u32 armv8pmu_pmcr_read(void) +{ + u64 val=0; + asm volatile("mrs %0, pmcr_el0" : "=r" (val)); + return (u32)val; +} +static inline void armv8pmu_pmcr_write(u32 val) +{ + val &= ARMV8_PMCR_MASK; + isb(); + asm volatile("msr pmcr_el0, %0" : : "r" ((u64)val)); +} + +static void +enable_cpu_counters(void* data) +{ + u32 val=0; +/* Enable user-mode access to counters. */ + asm volatile("msr pmuserenr_el0, %0" : : "r"((u64)ARMV8_PMUSERENR_EN_EL0|ARMV8_PMUSERENR_ER|ARMV8_PMUSERENR_CR)); +/* Initialize & Reset PMNC: C and P bits. */ + armv8pmu_pmcr_write(ARMV8_PMCR_P | ARMV8_PMCR_C); +/*G4.4.11 +PMINTENSET, Performance Monitors Interrupt Enable Set register */ +/*cycle counter overflow interrupt request is disabled */ + asm volatile("msr pmintenset_el1, %0" : : "r" ((u64)(0 << 31))); +/*start*/ + armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMCR_E); +} + +static void +disable_cpu_counters(void* data) +{ + u32 val=0; + printk(KERN_INFO "\n [" DRVR_NAME "] disabling user-mode PMU access on CPU #%d", + smp_processor_id()); + + /* Program PMU and disable all counters */ + armv8pmu_pmcr_write(armv8pmu_pmcr_read() |~ARMV8_PMCR_E); + /* disable user-mode access to counters. */ + asm volatile("msr pmuserenr_el0, %0" : : "r"((u64)0)); + +} + +static int __init +init(void) +{ + on_each_cpu(enable_cpu_counters, NULL, 1); + printk(KERN_INFO "[" DRVR_NAME "] initialized"); + return 0; +} + +static void __exit +fini(void) +{ + on_each_cpu(disable_cpu_counters, NULL, 1); + printk(KERN_INFO "[" DRVR_NAME "] unloaded"); +} + +MODULE_AUTHOR("Yogesh Tillu "); +MODULE_DESCRIPTION("Enables user-mode access to ARMv8 PMU counters"); +MODULE_VERSION("0:0.1-dev"); +module_init(init); +module_exit(fini);