From patchwork Tue Aug 5 14:48:33 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Fuzzey X-Patchwork-Id: 4679401 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 8D38EC0338 for ; Tue, 5 Aug 2014 14:51:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DBBC6201BC for ; Tue, 5 Aug 2014 14:51:49 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 27B5C2018A for ; Tue, 5 Aug 2014 14:51:46 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XEg30-00068h-M4; Tue, 05 Aug 2014 14:49:06 +0000 Received: from mta1.parkeon.com ([91.121.43.66]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XEg2s-000637-Gq for linux-arm-kernel@lists.infradead.org; Tue, 05 Aug 2014 14:49:00 +0000 Received: from ip71.parkeon.com ([213.152.31.71] helo=mta2.parkeon.com) by mta1.parkeon.com with esmtp (Exim 4.76) (envelope-from ) id 1XEg2T-0004hB-WA; Tue, 05 Aug 2014 16:48:34 +0200 Received: from mail.besancon.parkeon.com ([10.32.16.23]) by mta2.parkeon.com with esmtp (Exim 4.77) (envelope-from ) id 1XEg2R-0004a1-RR; Tue, 05 Aug 2014 16:48:31 +0200 Received: from [10.32.51.161] (port=40316 helo=[127.0.0.1]) by mail.besancon.parkeon.com with esmtp (Exim 4.71) (envelope-from ) id 1XEg2T-0004ch-Rm; Tue, 05 Aug 2014 16:48:33 +0200 Subject: [PATCH V2 1/4] ARM: perf: Set suniden bit. To: Shawn Guao , Will Deacon , linux-arm-kernel@lists.infradead.org From: Martin Fuzzey Date: Tue, 05 Aug 2014 16:48:33 +0200 Message-ID: <20140805144833.25462.46011.stgit@localhost> In-Reply-To: <20140805144831.25462.18149.stgit@localhost> References: <20140805144831.25462.18149.stgit@localhost> User-Agent: StGit/0.16 MIME-Version: 1.0 X-Virus-Scanned: by ClamAV at mta2.parkeon.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140805_074858_724615_4C9FE534 X-CRM114-Status: GOOD ( 16.80 ) X-Spam-Score: -0.7 (/) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Counters other than the CPU cycle counter only work if the security module SUNIDEN bit is set. Since access to this register is only possible in secure mode it will only be done if the device tree property "secure-reg-access" is set. Without this: # perf stat -e cycles,instructions sleep 1 Performance counter stats for 'sleep 1': 14606094 cycles # 0.000 GHz 0 instructions # 0.00 insns per cycle Some platforms (eg i.MX53) may also need additional platform specific setup. Signed-off-by: Martin Fuzzey --- Documentation/devicetree/bindings/arm/pmu.txt | 7 ++++ arch/arm/include/asm/pmu.h | 10 ++++++ arch/arm/kernel/perf_event_cpu.c | 3 ++ arch/arm/kernel/perf_event_v7.c | 44 +++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/Documentation/devicetree/bindings/arm/pmu.txt b/Documentation/devicetree/bindings/arm/pmu.txt index 75ef91d..7dab77e 100644 --- a/Documentation/devicetree/bindings/arm/pmu.txt +++ b/Documentation/devicetree/bindings/arm/pmu.txt @@ -27,6 +27,13 @@ Optional properties: - qcom,no-pc-write : Indicates that this PMU doesn't support the 0xc and 0xd events. +- secure-reg-access : Indicates that secure mode access is available. + This will cause the driver to do any setup required that + is only possible in secure mode. + If not present the secure registers will not be touched, + which means the PMU may fail to operate unless external + code (bootloader or security monitor) has performed the + appropriate initialisation. Example: pmu { diff --git a/arch/arm/include/asm/pmu.h b/arch/arm/include/asm/pmu.h index ae1919b..dfb654f 100644 --- a/arch/arm/include/asm/pmu.h +++ b/arch/arm/include/asm/pmu.h @@ -89,6 +89,16 @@ struct arm_pmu { u64 max_period; struct platform_device *plat_device; struct pmu_hw_events *(*get_hw_events)(void); + + /* + * Bits indicating any CPU or platform specific activations that have + * been done so we can undo them when stopping + */ + struct { + unsigned int secure_regs_available : 1; + unsigned int secure_debug_requested : 1; + unsigned int platform_enabled : 1; + } activated_flags; }; #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu)) diff --git a/arch/arm/kernel/perf_event_cpu.c b/arch/arm/kernel/perf_event_cpu.c index af9e35e..c09e18e 100644 --- a/arch/arm/kernel/perf_event_cpu.c +++ b/arch/arm/kernel/perf_event_cpu.c @@ -313,6 +313,9 @@ static int cpu_pmu_device_probe(struct platform_device *pdev) cpu_pmu->plat_device = pdev; if (node && (of_id = of_match_node(cpu_pmu_of_device_ids, pdev->dev.of_node))) { + pmu->activated_flags.secure_regs_available = + of_property_read_bool(pdev->dev.of_node, + "secure-reg-access"); init_fn = of_id->data; ret = init_fn(pmu); } else { diff --git a/arch/arm/kernel/perf_event_v7.c b/arch/arm/kernel/perf_event_v7.c index 1d37568..5ad9626 100644 --- a/arch/arm/kernel/perf_event_v7.c +++ b/arch/arm/kernel/perf_event_v7.c @@ -1377,12 +1377,47 @@ static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev) return IRQ_HANDLED; } +#define SDER_SUNIDEN (1 << 1) + +static inline u32 armv2pmu_read_sder(void) +{ + u32 sder; + + asm volatile("mrc p15, 0, %0, c1, c1, 1" : "=r" (sder)); + + return sder; +} + +static inline void armv2pmu_write_sder(u32 sder) +{ + asm volatile("mcr p15, 0, %0, c1, c1, 1" : : "r" (sder)); +} + static void armv7pmu_start(struct arm_pmu *cpu_pmu) { unsigned long flags; struct pmu_hw_events *events = cpu_pmu->get_hw_events(); raw_spin_lock_irqsave(&events->pmu_lock, flags); + + /* + * Counters other than cycle counter require SUNIDEN bit set + * Set the bit if the DT configuration allows it (secure mode) + * Otherwise do nothing and hope bootloader / secure monitor did the + * setup. + */ + if (cpu_pmu->activated_flags.secure_regs_available) { + u32 sder = armv2pmu_read_sder(); + + if (sder & SDER_SUNIDEN) { + cpu_pmu->activated_flags.secure_debug_requested = 0; + } else { + sder |= SDER_SUNIDEN; + armv2pmu_write_sder(sder); + cpu_pmu->activated_flags.secure_debug_requested = 1; + } + } + /* Enable all counters */ armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E); raw_spin_unlock_irqrestore(&events->pmu_lock, flags); @@ -1394,6 +1429,15 @@ static void armv7pmu_stop(struct arm_pmu *cpu_pmu) struct pmu_hw_events *events = cpu_pmu->get_hw_events(); raw_spin_lock_irqsave(&events->pmu_lock, flags); + + if (cpu_pmu->activated_flags.secure_debug_requested) { + u32 sder = armv2pmu_read_sder(); + + sder &= ~SDER_SUNIDEN; + armv2pmu_write_sder(sder); + cpu_pmu->activated_flags.secure_debug_requested = 0; + } + /* Disable all counters */ armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E); raw_spin_unlock_irqrestore(&events->pmu_lock, flags);